目录
[344. 反转字符串](#344. 反转字符串)
[541. 反转字符串 II](#541. 反转字符串 II)
[54. 替换数字(第八期模拟笔试)](#54. 替换数字(第八期模拟笔试))
344. 反转字符串
cpp
class Solution {
public:
void reverseString(vector<char>& s) {
for(int i=0;i<s.size()/2;i++){
swap(s[i],s[s.size()-1-i]);
}
}
};
541. 反转字符串 II
法一:
cpp
class Solution {
public:
string reverseStr(string s, int k) {
int p=0;
while(p<s.size()){
int right=p+k;
right=min(right,(int)s.size());
for(int i=p;i<p+(right-p)/2;i++){
swap(s[i],s[right-1-i+p]);
}
p+=k*2;
}
return s;
}
};
法二:
cpp
class Solution {
public:
string reverseStr(string s, int k) {
for(int i=0;i<s.size();i+=k*2){
int left=i;
int right=min(i+k-1,(int)s.size()-1);
while(left<right){
swap(s[left++],s[right--]);
}
}
return s;
}
};
54. 替换数字(第八期模拟笔试)
cpp
#include<iostream>
using namespace std;
int main(){
string s;
cin>>s;
string ns;
for(char t:s){
if(t>='0'&&t<='9'){
ns+="number";
}
else {
ns+=t;
}
}
cout<<ns;
return 0;
}
