-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate array.py
More file actions
33 lines (27 loc) · 965 Bytes
/
Rotate array.py
File metadata and controls
33 lines (27 loc) · 965 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
27
28
29
30
31
32
33
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# n=len(nums) # soln 1
# k=k%n
# nums[:]=nums[n-k:]+nums[:n-k]
def rev(arr: List[int],start: int,end: int) -> None: # soln 2
while(start<end):
arr[start],arr[end]=arr[end],arr[start]
start=start+1
end=end-1
l=len(nums)
k=k%l
if (l<=1):
return
else:
rev(nums,0,l-1)
rev(nums,0,k-1)
rev(nums,k,l-1)
# while(k>0):-
# temp=nums[-1] # brute force but have tle
# l=len(nums)
# for i in range(l-2,-1,-1):
# nums[i+1]=nums[i]
# nums[0]=temp
# k=k-1