leetcode 541 反转字符串 II Reverse String II python 最简代码 字符串的切片和反序不能同时操作

class Solution:
    def reverseStr(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: str
        """
        # method one 字符串的切片和反序操作不能同时进行
        res = ''
        for i in range(0,len(s),2*k):
            res = res + s[i:i+k][::-1] + s[i+k:i+2*k]
        return res

猜你喜欢

转载自blog.csdn.net/huhehaotechangsha/article/details/80852225