Python Solution (Two Pointers)


  • 0

    I put all the border judgment to the exception handling

        def moveZeroes(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
    
            p0, p1= 0, 0  #p0:point Zero,  p1:point Non-Zero
    
            while True:
                try:
                    while nums[p1] == 0: p1 += 1
                    while nums[p0] != 0: p0 += 1
    
                    if p0 < p1:
                        nums[p0], nums[p1] = nums[p1], nums[p0]
                    else:
                        p1 = p0
    
                except:
                    break
    

Log in to reply
 

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