Easy to understand Python Code


  • 0
    G
    class Solution(object):
        def moveZeroes(self, nums):
            index=0
            for item in range(len(nums)):
                if nums[index] == 0:
                   del nums[index]
                   nums.append(0)
                else:
                    index+=1
    
    

    Another way is to use .pop [O(1)] instead of del[O(n)]. Please correct me if I am wrong.

    class Solution(object):
        def moveZeroes(self, nums):
            index=0
            for item in range(len(nums)):
                if nums[index] == 0:
                   nums.pop(index)
                   nums.append(0)
                else:
                    index+=1
    
    

Log in to reply
 

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