541. 反轉字符串 II

class Solution {
public:
string reverseStr(string s, int k) {
for(int i = 0; i < s.size(); i += 2*k){
if(s.size() - i >= k){
reverse(s, i, i + k -1);
}else{
reverse(s, i, s.size() - 1);
}
}
return s;
}
void reverse(string& s, int head, int tail){
while(head < tail){
char temp = s[head];
s[head] = s[tail];
s[tail] = temp;
++head;
--tail;
}
}
};
浙公網安備 33010602011771號