344. 反转字符串
LeetCode题目
注意事项
class Solution {
public:
void reverseString(vector<char>& s) {
for (int i = 0; i < s.size() / 2; i++) {
char tmp = s[i];
s[i] = s[s.size()-1-i];
s[s.size()-1-i] = tmp;
}
}
};
541. 反转字符串 II
LeetCode题目
class Solution {
public:
string reverseStr(string s, int k) {
int num = 0;
while (num < s.size()) {
if ((s.size() - num) > 2 * k) {
reverse(s.begin()+num, s.begin()+num+k);
num += 2 * k;
}
else if ((s.size() - num) >= k) {
reverse(s.begin()+num, s.begin()+num+k);
break;
}
else {
reverse(s.begin()+num, s.end());
break;
}
}
return s;
}
};
54.替换数字
题目
#include <iostream>
using namespace std;
int main() {
string s;
while(cin >> s) {
int count = 0; // 统计数字的个数
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9') {
count++;
}
}
int pointer1 = s.size()-1, pointer2 = s.size()+5*count - 1;
s.resize(s.size()+5*count);
while(pointer1 >= 0) {
if (s[pointer1] >= '0' && s[pointer1] <= '9') {
s[pointer2--] = 'r';
s[pointer2--] = 'e';
s[pointer2--] = 'b';
s[pointer2--] = 'm';
s[pointer2--] = 'u';
s[pointer2--] = 'n';
}
else s[pointer2--] = s[pointer1];
pointer1--;
}
cout << s << endl;
}
}