Share my clear Python code to resolve this.


  • -1
    M
    def rotate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            if nums == []:
                pass
            else:
                K = k % len(nums)
                for i,n in enumerate(nums[-K:]+nums[:-K]):
                    nums[i] = n

  • 0

    this is not O(1) extra space. nums[::-K] will allocate a temporary space for it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.