剑指 Offer 58 - II. 左旋转字符串

题目:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。  简单
方法:切片  时间复杂度O(n)  空间复杂度O(n)  

def reverseLeftWords(s, n):
        """
        :type s: str
        :type n: int
        :rtype: str
        """
        if n > len(s):
            n %= len(s)
        left = s[:n]
        right = s[n:]
        return right + left

 

posted @ 2022-08-01 21:59  Liang-ml  阅读(22)  评论(0)    收藏  举报