-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0189-rotate_array.py
More file actions
45 lines (33 loc) · 933 Bytes
/
Copy path0189-rotate_array.py
File metadata and controls
45 lines (33 loc) · 933 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
39
40
41
42
43
44
45
"""
https://leetcode.com/problems/rotate-array
Given an integer array nums, rotate the array to the right by k steps,
where k is non-negative.
Example 1:
Input: nums = [1,2,3,4,5,6,7], 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]
"""
from typing import List
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
def reverse(arr, i, j):
while i < j:
arr[i], arr[j] = arr[j], arr[i]
i += 1
j -= 1
n = len(nums)
k = k % n
end = n - 1
middle = end - k
reverse(nums, 0, middle)
reverse(nums, middle + 1, end)
reverse(nums, 0, end)
nums = [1, 2, 3, 4, 5, 6, 7]
Solution().rotate(nums, 3)
print(nums)
nums = [1, 2]
Solution().rotate(nums, 3)
print(nums)