Download 10-week-algorithm-excercise-master/Week_01/189/rotate_array2.py from Dorothydu/gitrepo_20: direct link, hf CLI and curl.
- Browser
- Download file 524 Bytes
-
https://huggingface.co/datasets/Dorothydu/gitrepo_20/resolve/main/10-week-algorithm-excercise-master/Week_01/189/rotate_array2.py
- Command line
-
hf download hf://datasets/Dorothydu/gitrepo_20/10-week-algorithm-excercise-master/Week_01/189/rotate_array2.py
-
curl -L -o rotate_array2.py https://huggingface.co/datasets/Dorothydu/gitrepo_20/resolve/main/10-week-algorithm-excercise-master/Week_01/189/rotate_array2.py
524 Bytes
| from typing import List | |
| class Solution: | |
| def rotate(self, nums: List[int], k: int) -> None: | |
| """ | |
| Do not return anything, modify nums in-place instead. | |
| """ | |
| def reverse(arr: List[int], start: int, end: int): | |
| while start < end: | |
| arr[start], arr[end] = arr[end], arr[start] | |
| start += 1 | |
| end -= 1 | |
| n = len(nums) | |
| k = k % n | |
| reverse(nums, 0, n - 1) | |
| reverse(nums, 0, k - 1) | |
| reverse(nums, k, n - 1) | |