189. 旋转数组

 

 代码一:

1 class Solution(object):
2     def rotate(self, nums, k):
3         """
4         :type nums: List[int]
5         :type k: int
6         :rtype: None Do not return anything, modify nums in-place instead.
7         """
8         nums[:] = nums[-k%len(nums):] + nums[0:-k%len(nums)]

代码二:

 1 class Solution(object):
 2     def rotate(self, nums, k):
 3         """
 4         :type nums: List[int]
 5         :type k: int
 6         :rtype: None Do not return anything, modify nums in-place instead.
 7         """
 8         while k > 0:
 9             nums.insert(0, nums.pop())
10             k -= 1
原文地址:https://www.cnblogs.com/panweiwei/p/12748831.html