My simple python solution without append/pop


  • 4
    S
        zero = 0
        for non_zero in range(len(nums)):
            if nums[non_zero] == 0:
                continue
    
            if nums[zero] == 0:
                nums[zero], nums[non_zero] = nums[non_zero], nums[zero]
    
            zero += 1

  • 0
    N
        zero, non_zero = 0, 0
        while non_zero < len(nums):
            if nums[non_zero] == 0:
                non_zero += 1
                continue
            else:
                nums[zero], nums[non_zero] = nums[non_zero], nums[zero]
            zero += 1
            non_zero += 1

  • 0

    said in My simple python solution without append/pop:

    zero = 0
    for non_zero in range(len(nums)):
        if nums[non_zero] == 0:
            continue
    
       
        nums[zero], nums[non_zero] = nums[non_zero], nums[zero]
    
        zero += 1

Log in to reply
 

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