Easy Python Solution using List Comprehension


  • 0
    D

    If you just do nums = ret it will do a shallow copy, meaning if you change nums it will change ret, and if you change ret it will change nums. List comprehension creates a deep copy so that we don't just have an array type pointing to a location like in the previously stated sentence.

    Thanks to my friend Kevin for helping me out ^_^ Let me know if my logic makes sense.

    class Solution(object):
        def rotate(self, nums, k):
            ret = [x for x in nums]             #Deep copy
            for i in range(0, len(nums)):
                nums[(i+k)%len(nums)] = ret[i] 
    

Log in to reply
 

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