day8 字符串1
344.反转字符串
class Solution {
public:
void reverseString(vector<char>& s) {
int left = 0;
int right = s.size() - 1;
while (left < right) {
swap(s[left], s[right]);
left ++;
right --;
}
}
};
541. 反转字符串II
class Solution {
public:
void reverseSub(string &s, int left, int right) {
right = right >= s.size() ? s.size() - 1 : right;
while (left < right) {
swap(s[left], s[right]);
left ++;
right --;
}
}
string reverseStr(string s, int k) {
int n = s.size();
int left = 0;
while (left < n) {
int right = left + k - 1;
reverseSub(s, left, right);
left = right + k + 1;
}
return s;
}
};
卡码网:54.替换数字
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main() {
string res;
char ch;
while (cin >> ch) {
if ('a' <= ch && ch <= 'z') {
res += ch;
}
else {
res += "number";
}
}
cout << res << endl;
return 0;
}