- 反转字符串
class Solution {
public:
void reverseString(vector<char>& s) {
int n = s.size();
for (int i = 0; i < n/2; i++){
char tmp = s[i];
s[i] = s[n-1-i];
s[n-1-i] = tmp;
}
}
};
- 反转字符串 II
class Solution {
public:
string reverseStr(string s, int k) {
for(int i = 0; i < s.size(); i += (2*k))
{
int be = i, ed = 0;
if( (i + k - 1) < s.size()) ed = i + k - 1;
else ed = s.size() - 1;
while( be < ed)
{
int tmp = s[be];
s[be] = s[ed];
s[ed] = tmp;
be++;
ed--;
}
}
return s;
}
};
- 反转字符串中的单词
class Solution {
public:
string reverseWords(string s) {
vector<string> ss;
string out_s;
int i = 0, j = 0;
while(i < s.size())
{
out_s = "";
while(i < s.size() && s[i] == ' ') i++;
while(i < s.size() && s[i] != ' ')
{
out_s = out_s + s[i];
i++;
}
if(out_s != "")
{
ss.push_back(out_s);
j++;
}
}
out_s = "";
out_s = ss[j-1];
for(int k = j - 2; k >= 0; k-- )
{
out_s = out_s + ' ' + ss[k];
}
return out_s;
}
};