-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189_RotateArray.py
More file actions
38 lines (36 loc) · 1.02 KB
/
189_RotateArray.py
File metadata and controls
38 lines (36 loc) · 1.02 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
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.
"""
def revert(start, end):
while start<end:
nums[start], nums[end] = nums[end], nums[start]
start+=1
end-=1
l = len(nums)
k %=l
revert(0, l-1-k)
revert(l-k,l-1)
revert(0,l-1)
def rotate2(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
i=0
while i <k:
tmp = nums.pop()
nums.insert(0,tmp)
i+=1
def rotate3(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = len(nums)
nums[:] = nums[n-k:] + nums[:n-k]