mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #1517 from 747063715/ReverseStringII
反转字符串II添加了一种C++解法
This commit is contained in:
@ -63,6 +63,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
那么我们也可以实现自己的reverse函数,其实和题目[344. 反转字符串](https://programmercarl.com/0344.反转字符串.html)道理是一样的。
|
||||
|
||||
下面我实现的reverse函数区间是左闭右闭区间,代码如下:
|
||||
@ -92,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;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user