344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
344. 翻转字符串
写一个翻转字符串的算法。
思路
使用两个index,一个从左到右,一个从右到左,将字符串中的字符交换。
代码
class Solution {
public:
string reverseString(string s) {
int i=0, j=s.size()-1;
while(i<j){
swap(s[i++], s[j--]);
}
return s;
}
};