标签:
#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: # @param nums, a list of integer # @param k, num of steps # @return nothing, please modify the nums list in-place. def rotate(self, nums, k): indexArr = [] #初始化一个列表 length = len(nums)#获得数组的长度 if k > length: k = k - length for i in range(0,k): indexArr.append(nums[length-1-i]) for i in range(0,length-k): nums[length-1-i] = nums[length-1-i-k] for i in range(0,k): nums[i] = indexArr[k-1-i]
标签:
原文地址:http://my.oschina.net/stevenKelly/blog/382665