方法1.拼接数组法
思路: 队尾各项 + 队首各项之和
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
n = k % len(nums)
nums[:] = nums[-n:] + nums[:-n]
方法2.直接法
思路: 队尾弹出再插入到队首
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
n = k % len(nums)
for _ in range(n):
nums.insert(0, nums.pop())