我是靠谱客的博主 深情烧鹅,这篇文章主要介绍Leetcode 剑指 Offer 58 - II. 左旋转字符串 LCOF - Python,现在分享给大家,希望可以做个参考。

class Solution:
    def reverseLeftWords(self, s: str, n: int) -> str:
        m = len(s)
        temp = list(s)
        temp.extend(['']*n)
        front, rear = 0, m
        for i in range(0,n):
            temp[rear] = temp[front]
            rear += 1
            front += 1
        return ''.join(temp[n:n+m])

第二个方法,先反转前k个,再反转k到结尾的所有字母,最后反转整个字符串

class Solution:
    def reverseLeftWords(self, s: str, n: int) -> str:
        return s[n:] + s[0:n]

最后

以上就是深情烧鹅最近收集整理的关于Leetcode 剑指 Offer 58 - II. 左旋转字符串 LCOF - Python的全部内容,更多相关Leetcode内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部