Python Solution


  • 0
    G

    I don't think I've seen anyone with this solution yet:

    class Solution(object):
        def moveZeroes(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            count = 0
            for x in range (0, len(nums)):
                if nums[x] == 0:
                    count += 1
                else:
                    nums[x], nums[x-count] = nums[x-count], nums[x]
    
    • I iterate through the list once, and whenever there is a zero I increment count

    • My count is giving the index of where my next non-zero number should go

    • This places zeros at the end while numbers at the front simultaneously


Log in to reply
 

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