Python code for rotating array

  • 0
    def rotate(nums, k):
        n = len(nums)
        if n<2 or k%n == 0:
        k = k%n
        for idx in xrange(k):
            temp = nums[-1] # Store the last element of nums 
            del nums[-1] # Delete the last element from nums
            print nums 
            # nums = [temp] + nums # Append the last element to the front of nums 
            # Does not mute the nums in the global scope
            print nums

Log in to reply

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