44ms Python code


  • 0
    P

    Keep two pointers, iterate, and swap as necessary.

    class Solution(object):
        def moveZeroes(self, nums):
            i, j = 0, 1
            while j < len(nums):
            	if nums[i] == 0:
            		if nums[j] != 0:
            			nums[i], nums[j] = nums[j], nums[i]
            			i += 1
            	else:
            		i += 1
            j += 1

Log in to reply
 

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