算法1
(双指针)
C++ 代码
class Solution {
public:
void reverseString(vector<char>& s) {
int i=0,j=s.size()-1;
int n=s.size();
for(int i=0;i<n/2;i++,j--){
swap(s[i],s[j]);
}
}
};
算法2
(递归)
C++ 代码
class Solution {
public:
void reverseString(vector<char>& s) {
if(s.size()==0) return;
reverse(s,0,s.size()-1);
}
void reverse(vector<char>&s,int left,int right){
if(left>=right) return;
reverse(s,left+1,right-1);
swap(s[left],s[right]);
}
};