LeetCode初级算法-数组-旋转数组(python)

旋转数组

题目地址

LeetCode初级算法-数组-旋转数组(python)_第1张图片


 

解答:

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.
        """
        assert k>=0,\
        'k must be postive'
        for i in range(k):
            nums.insert(0,nums.pop())

 

你可能感兴趣的:(Leetcode)