力扣题:字符串的反转-11.22
力扣题-11.22
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:541. 反转字符串 II
解题思想:进行遍历翻转即可
class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
t = list(s)
for i in range(0, len(t), 2 * k):
t[i: i + k] = reversed(t[i: i + k])
return "".join(t)
class Solution {
public:
string reverseStr(string s, int k) {
int n = s.length();
for (int i = 0; i < n; i += 2 * k) {
reverse(s.begin() + i, s.begin() + min(i + k, n));
}
return s;
}
};