4 lines solution in Python


  • 0
    T
    def moveZeroes(nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            for i in nums:
                if i == 0:
                    nums.remove(i)
                    nums.append(0)
    

    because the length of nums won't change, my iteration will execute exactly len(nums) times.


  • 0

    Remove is O(n). Total complexity is O(n^2).


Log in to reply
 

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