lc344反转字符串
文章:lc344
代码:
class Solution {
public:
void my_swap(char &a, char &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
void reverseString(vector<char>& s) {
int i = 0;
int j = s.size() - 1;
while(i < j) {
my_swap(s[i], s[j]);
i++;
j--;
}
}
};
lc541反转字符串II
文章:lc541
代码:
class Solution {
public:
string reverseStr(string s, int k) {
for (int i=0; i < s.size(); i+=(2*k)) {
if (s.begin() + i + k < s.end()) {
reverse(s.begin()+i, s.begin()+i+k);
}
else {
reverse(s.begin()+i, s.end());
}
}
return s;
}
};
kama54 替换数字
文章: kama54
代码:
# include <iostream>
# include <string>
using namespace std;
int main() {
string str;
string result;
cin >> str;
for (auto s : str) {
if (s - '0' >= 0 && s - '0' <= 9) {
result += "number";
}
else {
result += s;
}
}
cout << result;
return 0;
}
kama55右旋字符串
文章:kama55
代码:
# include <iostream>
# include <string>
# include <algorithm>
using namespace std;
int main() {
int n;
string s;
cin >> n;
cin >> s;
reverse(s.begin(), s.end());
reverse(s.begin(), s.begin() + n);
reverse(s.begin() + n, s.end());
cout << s;
return 0;
}