Difficulty: Easy
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
class Solution {
public:
string reverseString(string s) {
int i=0, j=s.size()-1;
char tmp;
for(;i<j ;i++, j--){
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
}
return s;
}
};
两个指针,从前往后与从后往前遍历并互换。