344.反转字符串
class Solution {
public:
void reverseString(vector<char>& s) {
for (int i = 0, j = s.size()-1; i < s.size() / 2; i++, j--) {
swap(s[i], s[j]);
}
}
};
541.反转字符串||
题目链接:541. 反转字符串 II - 力扣(LeetCode)
class Solution {
public:
string reverseStr(string s, int k) {
for (int i = 0; i < s.size(); i += 2*k) {
if (i + k <= s.size()) {
reverse(s.begin() + i, s.begin() + i + k);
//是从s.begin(),字符串的前面开始往后交换
}
else reverse(s.begin() + i, s.end());
//这里是不满2k的从该字符串s.begin()到字符串的最后进行交换
}
return s;
}
};
替换数字
#include <iostream>
#include <string>
using namespace std;
int main() {
char c;
while (cin >> c) { // 逐字符读取(跳过空格/换行)
if (isdigit(c)) {
cout << "number"; // 直接替换数字
} else {
cout << c; // 非数字原样输出
}
}
return 0;
}
882

被折叠的 条评论
为什么被折叠?



