(1)541. 反转字符串II
题目描述:
间隔K个字符反转一下区间内的字符
输入: s = “abcdefg”, k = 2
输出: “bacdfeg”
解题思路:
按照K个步长在2K的区间中操作数据即可
class Solution:
def reverseStr(self, s: str, k: int) -> str:
left, mid, right = 0, k, 2 * k # 初始化左中右指针
res = '' # 初始化结果字符串
while len(res) < len(s): # 满足条件时执行
res += s[left:mid][::-1] + s[mid:right] # 把当前单元的结果添加到结果字符串
left, mid, right = left + 2 * k, mid + 2 * k, right + 2 * k
return res