Readable Python solution [55ms]


  • 0
    class Solution(object):
        def moveZeroes(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            count = len(nums)
            
            # Pack non-zero elements to the head of list
            w = 0
            for i in xrange(count):
                value = nums[i]
                if value:
                    nums[w] = value
                    w += 1
            
            # Fill the rest with zeros
            while w < count:
                nums[w] = 0
                w += 1
    

Log in to reply
 

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