Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Note:1.注意空間複雜度,不需要重新分配內存,直接在原字符串上進行操作。 2.swap()函數的使用, swap函數原型如下:
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
class Solution {
public:
string reverseString(string s) {
int i=0, j = s.length()-1;
while(i<j) swap(s[i++],s[j--]);
return s;
}
};