-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189_Rotate_Array.py
More file actions
59 lines (44 loc) · 1.45 KB
/
189_Rotate_Array.py
File metadata and controls
59 lines (44 loc) · 1.45 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
"""
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
"""
class Solution:
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
for i in range(k%len(nums)):
tmp = nums.pop()
nums.insert(0,tmp)
print(nums)
"""
The previous one can truly change the value of old nums,
but the following one just changes its reference to a new nums not the value of old nums.
从最后一位竖起,往前推k位,最为新数组的第一部分
从第一位开始,到底k位,作为数组的第二部分
合并即可,必须要用nums[:]
"""
def rotate2(self, nums, k):
l = len(nums)
k = k % l
nums[:] = nums[l-k:] + nums[:l-k] #这里必须是nums[:]而不是nums,因为nums只改变形参
so = Solution()
nums = [1,2,3,4,5,6,7] ;k = 8
# nums = [1,2,3] ;k = 2
rs = so.rotate(nums,k)