1. 程式人生 > >#Leetcode# 344. Reverse String

#Leetcode# 344. Reverse String

https://leetcode.com/problems/reverse-string/

 

Write a function that takes a string as input and returns the string reversed.

Example 1:

Input: "hello"
Output: "olleh"

Example 2:

Input: "A man, a plan, a canal: Panama"
Output: "amanaP :lanac a ,nalp a ,nam A"

程式碼:

class Solution {
public:
    string reverseString(string s) {
        int len = s.length();
        for(int i = 0; i < len / 2; i ++)
            swap(s[i], s[len - i - 1]);
        return s;
    }
};