python solution easy to understand


  • 0
    S
    class Solution(object):
        def reverseStr(self, s, k):
            """
            :type s: str
            :type k: int
            :rtype: str
            """
            s = list(s)
            start = 0
            while start<len(s):
                if len(s)-1-start<k:
                    s[start:len(s)] = s[start:len(s)][::-1]
                    return ''.join(s)
                if len(s)-1-start>=k and len(s)-1-start<2*k:
                    s[start:start+k] = s[start:start+k][::-1]
                    return ''.join(s)
                if len(s)-1-start>=2*k:
                    s[start:start+k] = s[start:start+k][::-1]
                    start += 2*k
            return ''.join(s)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.