呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 189 Rotate Array

文档信息

leetcode 189 Rotate Array

解法一

class Solution(object):
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        k = k % len(nums)
        
        def reverseSublist(lst, start, end):
            lst[start:end] = lst[start:end][::-1]
        
        reverseSublist(nums, 0, len(nums))
        reverseSublist(nums, 0, k)
        reverseSublist(nums, k, len(nums))

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub