-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path189_RotateArray.py
More file actions
81 lines (63 loc) · 2.06 KB
/
189_RotateArray.py
File metadata and controls
81 lines (63 loc) · 2.06 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
71
72
73
74
75
76
77
78
79
80
81
# coding: utf8
"""
题目链接: https://leetcode.com/problems/rotate-array/description.
题目描述:
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
[show hint]
Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
"""
class Solution(object):
def rotate_v1(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
if not nums or k <= 0:
return
ll = len(nums)
nums_slice = nums[:]
for i in range(ll):
nums[(i + k) % ll] = nums_slice[i]
def rotate_v2(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
if not nums or k <= 0:
return
ll = len(nums)
start = 0
k %= ll
while k > 0:
for i in range(k):
nums[start + i], nums[start + ll - k + i] = nums[start + ll - k + i], nums[start + i]
start += k
ll -= k
k %= ll
def rotate_v3(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
if not nums or k <= 0:
return
def reverse_array(array, start, end):
while end > start:
array[end], array[start] = array[start], array[end]
start += 1
end -= 1
ll = len(nums)
k %= ll
reverse_array(nums, 0, ll - 1)
reverse_array(nums, 0, k - 1)
reverse_array(nums, k, ll - 1)