-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path189_RotateArray.py
63 lines (57 loc) · 1.52 KB
/
189_RotateArray.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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
# 1. Make an extra copy and then rotate.
def rotate(self, nums, k):
if not nums or k <= 0:
return
nums_copy = nums[:]
n = len(nums)
for i in range(n):
nums[(i+k) % n] = nums_copy[i]
# 2. Simple three reverse.
# Reverse all the n elements, the first k elements,
# and then the last n-k elements.
def rotate_1(self, nums, k):
if not nums or k <= 0:
return
n = len(nums)
k = k % n
nums.reverse()
self.reverse_part(nums, 0, k-1)
self.reverse_part(nums, k, n-1)
def reverse_part(self, nums, start, end):
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
"""
3. Swap the last k elements with the first k elements.
The last k elements will be in the correct positions.
but we need to rotate the remaining (n - k) elements
to the right position.
Now we need to rotate(nums[k:], k), then keep rotating
"""
def rotate_2(self, nums, k):
if not nums or k <= 0:
return
n = len(nums)
k = k % n
start = 0
while k:
for i in range(k):
nums[start+i], nums[start+n-k+i] = (
nums[start+n-k+i], nums[start+i])
start += k
n = n - k
k = k % n
"""
[1]
0
[1,2]
1
[1,2,3,4,5,6,7]
3
[1,2,3,4,5,6,7]
10
"""