My simple solution using python


  • 0
    L
    class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
    
        # iterate from end to the begining, and find the index of 0, then slid from index+1 until reach 0
        if not nums:
            return
        s = len(nums) - 1
        for i in range(s, -1, -1):
            if nums[i] == 0:
                j = i + 1
                while j <= s and nums[j] != 0:  # until reach 0 
                    nums[j - 1] = nums[j]  # slid
                    j += 1
                nums[j - 1] = 0  # do not forget set the last value to 0 
                pass
            pass

Log in to reply
 

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