美文网首页
Leetcode-189题:Rotate Array

Leetcode-189题:Rotate Array

作者: 八刀一闪 | 来源:发表于2016-09-26 21:57 被阅读53次

    题目

    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

          本文链接:https://www.haomeiwen.com/subject/snoqyttx.html