leetcode 189. 旋转数组
https://leetcode-cn.com/problems/rotate-array/
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
n=len(nums)
if n<=1:
return nums
k=k%n
start_index=0
change_num=0 #总共需要改变n个数共n次位置
while change_num<n:
next_pos=(start_index+k)%n
tmp=nums[start_index]
while next_pos!=start_index:
tmp,nums[next_pos]=nums[next_pos],tmp
change_num+=1
next_pos=(next_pos+k)%n
nums[next_pos]=tmp
change_num+=1
start_index+=1
return nums