Update 0541.反转字符串II.md

This commit is contained in:
程序员Carl
2022-07-26 09:17:40 +08:00
committed by GitHub
parent f5e1834439
commit fdf0185e1d

View File

@ -64,22 +64,7 @@ public:
```
```
class Solution {
public:
string reverseStr(string s, int k) {
int n=s.size(),pos=0;
while(pos<n){
//剩余字符串大于等于k的情况
if(pos+k<n) reverse(s.begin()+pos,s.begin()+pos+k);
//剩余字符串不足k的情况
else reverse(s.begin()+pos,s.end());
pos+=2*k;
}
return s;
}
};
```
那么我们也可以实现自己的reverse函数其实和题目[344. 反转字符串](https://programmercarl.com/0344.反转字符串.html)道理是一样的。
@ -110,7 +95,24 @@ public:
```
另一种思路的解法
```CPP
class Solution {
public:
string reverseStr(string s, int k) {
int n = s.size(),pos = 0;
while(pos < n){
//剩余字符串大于等于k的情况
if(pos + k < n) reverse(s.begin() + pos, s.begin() + pos + k);
//剩余字符串不足k的情况
else reverse(s.begin() + pos,s.end());
pos += 2 * k;
}
return s;
}
};
```