Python code for rotating array


  • 0
    A
    def rotate(nums, k):
        n = len(nums)
        if n<2 or k%n == 0:
            return;
                
        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
            nums.insert(0,temp)
            print nums
    

Log in to reply
 

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