Rotate Array
阿新 • • 發佈:2017-09-22
lis rotate range ram image pop in-place src ()
這道題為簡單題
題目:
思路:
我的思路:利用列表的insert和pop方法進行操作
大神:利用分片操作,效率明顯高得多
代碼:
我的代碼:
1 class Solution(object): 2 def rotate(self, nums, k): 3 """ 4 :type nums: List[int] 5 :type k: int 6 :rtype: void Do not return anything, modify nums in-place instead.7 """ 8 for i in range(k): 9 nums.insert(0, nums.pop())
大神的:
1 class Solution: 2 # @param nums, a list of integer 3 # @param k, num of steps 4 # @return nothing, please modify the nums list in-place. 5 def rotate(self, nums, k): 6 n = len(nums)7 k = k % n 8 nums[:] = nums[n-k:] + nums[:n-k]
Rotate Array