思路:
每次移动2k位,判断是否超过数组,超过则全部反转,没超过则反转到第i+k个
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);
}
//超出数组反转全部
else {
reverse(s.begin() + i, s.end());
}
}
return s;
}
};
标签:string,int,反转,II,541,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16617053.html