lc189. 轮转数组
class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ if k >= len(nums): rotate_times = k % len(nums) while rotate_times > 0: i = len(nums) - 1 j = i-1 temp = nums[i] while j >=0: nums[j+1] = nums[j] j -= 1 nums[0] = temp rotate_times -= 1 else: nums_temp = nums[:] for i in range(0,len(nums_temp)): if i+k < len(nums): nums[i+k] = nums_temp[i] else: nums[(i+k) % len(nums)] = nums_temp[i]
浙公网安备 33010602011771号