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.
My code:
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.
"""
n = len(nums)
k = k % n
nums[:] = nums[n-k:] + nums[:n-k]
print(nums)
def rotate0(self,nums,k):
n = len(nums)
idx = 0
distance = 0
cur = nums[0]
for x in range(n):
idx = (idx + k) % n
nums[idx], cur = cur, nums[idx]
distance = (distance + k) % n
if distance == 0:
idx = (idx + 1) % n
cur = nums[idx]
reference:http://bookshadow.com/weblog/2015/02/24/leetcode-rotate-array/