- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189. Rotate Array.py
More file actions
Latest commit
38 lines (33 loc) · 959 Bytes
/
Copy path189. Rotate Array.py
File metadata and controls
38 lines (33 loc) · 959 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
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
# @Time : 2019/2/26 15:21
# @Author : xulzee
# @Email : xulzee@163.com
# @File : 189. Rotate Array.py
# @Software: PyCharm
fromtypingimportList
classSolution(object):
defrotate1(self, nums: List[int], k: int) ->None:
"""
Do not return anything, modify nums in-place instead.
"""
foriinrange(k):
nums.insert(0, nums.pop())
defrotate(self, nums: List[int], k: int) ->None:
"""
Do not return anything, modify nums in-place instead.
"""
# l = len(nums)
# k %= l
# nums[:l-k] = nums[:l-k][::-1]
# nums[l-k:] = nums[l-k:][::-1]
# nums[:] = nums[::-1]
l=len(nums)
k%=l
nums[:l] =nums[:l][::-1]
nums[:k] =nums[:k][::-1]
nums[k:l] =nums[k:l][::-1]
if__name__=='__main__':
A= [1, 2, 3, 4, 5, 6, 7]
k=3
Solution().rotate(A, k)
print(A)