解题思路,把数组轮换想成栈的出栈后又入栈即可,当然要判断好K的量,因为K有可能会超过数组长度
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k %= n
for _ in range(k):
nums.insert(0, nums.pop())
return nums