给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
示例 1:
输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,5] 向右旋转 3 步: [5,6,7,1,2,3,4]
示例 2:
输入: [-1,-100,3,99] 和 k = 2 输出: [3,99,-1,-100] 解释: 向右旋转 1 步: [99,-1,-100,3] 向右旋转 2 步: [3,99,-1,-100]
说明:
尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。 要求使用空间复杂度为 O(1) 的原地算法。Solution:(因为要求是原地修改,所以不能使用切片的方法创建一个新数组,利用数组元素的弹出和插入来实现(对k取余是为了防止出现k大于数组长度时进行多余的操作。))
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. """ k = k % len(nums) for i in range(k): element = nums.pop() nums.insert(0, element) return nums solution = Solution() print(solution.rotate([1, 2, 3, 4, 5, 6], 3)) [4, 5, 6, 1, 2, 3]