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].

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

[show hint]

Related problem: Reverse Words in a String II


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







posted @ 2017-08-08 23:35  xiejunzhao  阅读(142)  评论(0编辑  收藏  举报