-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
26 lines (23 loc) · 784 Bytes
/
solution.py
File metadata and controls
26 lines (23 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
"""
# this way use extra space
length = len(nums)
# avoid repeat
k = k % length
count = 0
index = 0
temp = nums[index]
rotate_map = {}
while count < length:
if rotate_map.get(index) != None:
index += 1
temp = nums[index]
rotate_map[index] = True
next_index = index + k if index + k < length else index + k - length
nums[next_index], temp, index = temp, nums[next_index], next_index
count += 1