344.反转字符串
https://leetcode.cn/problems/reverse-string/description/
class Solution {
public:
void reverseString(vector<char>& s) {
int i = 0, j = s.size()-1;
char tmp;
while (i < j){
// s[i], s[j] = s[j], s[i]; // it does not work
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
}
};
541.反转字符串II
class Solution {
public:
string reverseStr(string s, int k) {
int total_rounds = s.size() / k;
int remaining_num = s.size() % k;
int i=0, j=k;
for (int round=0; round<total_rounds; round++){
if (round%2 == 0){
i = round*k+0, j=round*k+k-1;
while (i<j){
swap(s[i], s[j]);
i++;
j--;
}
}
}
if (total_rounds%2==0){
i = total_rounds*k+0, j=s.size()-1;
while (i<j){
swap(s[i], s[j]);
i++;
j--;
}
}
return s;
}
};
151. 反转字符串里面的单词
https://leetcode.cn/problems/reverse-words-in-a-string/
暂时还没写出来,之后补一下吧