My two Python solutions, just for fun


  • 0
    M
    #its a very simple and understandable, 52ms
    def moveZeroes(self, nums):
        if len(nums) == 0:
            return
        position = 0
        for i in xrange(len(nums)):
            if nums[i]:
                nums[position] = nums[i]
                position += 1
        for i in xrange(position, len(nums)):
            nums[i] = 0
    
    
    #64ms, use pop to delete 0's
    def moveZeroes_pop(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        if len(nums) == 0:
            return
        index, count = 0, 0
        while index <len(nums):
            if nums[index] == 0:
                nums.pop(index)
                count += 1
                continue
            index += 1
        nums.extend([0]*count)

Log in to reply
 

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