-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path189. Rotate Array.py
38 lines (33 loc) · 959 Bytes
/
189. Rotate Array.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
# -*- coding: utf-8 -*-
# @Time : 2019/2/26 15:21
# @Author : xulzee
# @Email : [email protected]
# @File : 189. Rotate Array.py
# @Software: PyCharm
from typing import List
class Solution(object):
def rotate1(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(k):
nums.insert(0, nums.pop())
def rotate(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)