forked from coldmanck/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0189_Rotate_Array.py
More file actions
25 lines (21 loc) · 790 Bytes
/
Copy path0189_Rotate_Array.py
File metadata and controls
25 lines (21 loc) · 790 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
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
'''time O(n) space O(1)'''
nums.reverse()
k %= len(nums)
left, right = 0, k - 1
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left, right = left + 1, right - 1
left, right = k, len(nums) - 1
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left, right = left + 1, right - 1
'''time = space = O(n)'''
# nums2 = nums[:]
# k %= len(nums)
# nums[:k] = nums2[len(nums) - k:]
# nums[k:] = nums2[:len(nums) - k]