没看答案,双指针局部翻转再拼接一起。
class Solution:
def reverseStr(self, s: str, k: int) -> str:
left, right = 0, 0
res = ''
while right < len(s):
right += k
if right >= len(s):
res += s[left:][::-1]
break
res += s[left:right][::-1]
left = right
right += k
if right >= len(s):
res += s[left:]
break
res += s[left:right]
left = right
return res