lettcode189Rotate Array

来源:互联网 发布:顺丰速运成本数据分析 编辑:程序博客网 时间:2024/06/06 00:21
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%=len(nums)
        self.rotateMethod(nums,0,len(nums)-1)
        self.rotateMethod(nums,0,k-1)
        self.rotateMethod(nums,k,len(nums)-1)
    
    def rotateMethod(self,nums,l,r):
        while l<r:
            tmp=nums[l]
            nums[l]=nums[r]
            nums[r]=tmp
            l+=1
            r-=1
0 0