-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrot.py
More file actions
36 lines (23 loc) · 982 Bytes
/
rot.py
File metadata and controls
36 lines (23 loc) · 982 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
34
35
36
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.
"""
"""
# 100 pass - Hacky pythonic solution of splitting the array and joining
n = len(nums)
# K is zero
if k == 0:
nums
else:
# Forsee this, k can be any integer, make k withing length of num
k = k%n
# Rotate - assigning new values with nums[:] only works
nums[:] = nums[n-k:]+nums[:n-k]
"""
# 100 pass - Pythonic solution using pop() - which removes the last element, remove k elements from the end of the array and append then to the front of the array
for i in range(k):
removed = nums.pop()
nums.insert(0,removed)