Leetcode-189题:Rotate Array

题目

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

代码

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.
        """
        if nums==None or len(nums)==0 or k<0:
            return
        t = nums[:]
        for i in range(len(t)):
            nums[(i+k)%len(t)] = t[i]

你可能感兴趣的:(Leetcode-189题:Rotate Array)