题源:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnhbqj/
题解一:直接调用STL中的函数即可。
代码:
class Solution {
public:
void reverseString(vector<char>& s) {
reverse(s.begin(),s.end());
}
};
题解二:使用双指针,一个指向字符串的首元素,一个指向字符串的尾元素,进行交换,向中间靠拢。
代码:
class Solution {
public:
void reverseString(vector<char>& s) {
int len=s.size();
for(int i=0,j=len-1;i<=len/2&&j>=len/2;i++,j--){
swap(s[i],s[j]);
}
}
};