-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path646.py
More file actions
70 lines (50 loc) · 1.45 KB
/
646.py
File metadata and controls
70 lines (50 loc) · 1.45 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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.
"""
"""
This solution doens't scale for memory usage
"""
if len(nums) == 1:
return
if k == 0:
return
def _rotate(nums, k):
resList = nums * k * 2 # This is the space killer
slidingWindowEnd = len(resList)
slidingWindowStart = len(resList) - len(nums)
for i in range(k, 0, -1):
print(slidingWindowStart, slidingWindowEnd)
slidingWindowStart -= 1
slidingWindowEnd -= 1
nums[:] = resList[slidingWindowStart:slidingWindowEnd]
# i = k
_rotate(nums, k)
# while i > 1:
# _rotate(nums, i)
# i -= 1
def pythonicSolution(nums, k):
k = k % len(nums)
nums[:] = nums[len(nums) - k:] + nums[:len(nums) - k]
# inputNums = [1,2,3,4,5,6,7]
# inputK = 3
inputNums = [1,2]
inputK = 1
sol = Solution()
sol.rotate(inputNums, inputK)
print(inputNums)
# resList = []
# def prints(a, n, ind):
# i = ind
# # print from ind-th index to (n+i)th index.
# while i < n + ind :
# resList.append(a[i % n])
# i = i + 1
# # Driver Code
# a = [-1,-100,3,99]
# n = len(a);
# prints(a, n, 2);
# print(resList)