Simple Python solution

  • 0
    class Solution(object):
        def moveZeroes(self, nums):
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            n = len(nums)
            i, j = 0, 0
            while True:
                while (i<n) and (not nums[i]==0):
                if i==n: return
                while (j<n) and (nums[j]==0):
                if j==n: return
                nums[i], nums[j] = nums[j], nums[i]

    By the way, if it wasn't required to do it in-place, then simple one liner is OK.

    return [n for n in nums if not n==0] + [n for n in nums if n==0]

Log in to reply

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