class Solution: def rotate(self, nums, k: int) -> None: """ Do not return anything, modify nums in-place instead. """ while k>0: a=nums.pop(-1) nums.insert(0,a) k-=1 return nums
执行用时 :168 ms, 在所有 Python3 提交中击败了27.45%的用户
内存消耗 :14.9 MB, 在所有 Python3 提交中击败了5.18%的用户
看看人家40ms的范例:
class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ k = k % len(nums) nums[:]= nums[len(nums)-k:] + nums[0:len(nums)-k]
多么简洁。。。
——2019.10.8