-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotate-array.py
36 lines (30 loc) · 1.06 KB
/
rotate-array.py
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
from typing import List
class Solution:
# Time complexity: O(n) where n is the length of nums
# Space complexity: O(1)
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
if k == 0: return
nums.reverse()
for i in range(k // 2):
nums[i], nums[k-1-i] = nums[k-1-i], nums[i]
for i in range((len(nums)-k) // 2):
nums[i+k], nums[len(nums)-1-i] = nums[len(nums)-1-i], nums[i+k]
# Relatively naive solution
# Time complexity: O(n)
# Space complexity: O(n)
def rotate2(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
if k == 0: return
tail = nums[len(nums)-k:]
head = nums[:len(nums)-k]
for i, el in enumerate(head):
nums[i+k] = el
for i, el in enumerate(tail):
nums[i] = el