题目:
将包含 n 个元素的数组向右旋转 k 步。
例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4]。
要求:
要求空间复杂度为 O(1)
(这里的意思就是别想着新建个列表)
解决方法一:
简单暴力点,将后面k个删掉插入到前面。
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
lenth = len(nums) - 1