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]
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
N = len(nums)
tmp = [0] * N
for i in range(N):
tmp[(i+k) % N] = nums[i]
nums[:] = tmp
๐ก Follow up
- in-place๋ฅผ O(1) extra space์ผ๋ก ํด๊ฒฐํ ์ ์๋๊ฐ?