Wrong answer with Python


  • 0
    X

    My answer in python is

    def moveZeroes(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            b = 0
            for i in nums:
                if i == 0:
                    nums.remove(i)
                    b += 1
            nums = nums + [0]*b
    

    However, when I try to submit, it shows the "wrong answer". I'm sure I successfully updated the array. I don't know why this problem would happen. Any ideas?


  • 0
    X

    If I print 'nums' at the last line, I would get:

    Input:
    [0,1,0,3,12]
    Output:
    [1,3,12]
    Expected:
    [1,3,12,0,0]
    Stdout:
    [1, 3, 12, 0, 0]
    

    Really confused.


Log in to reply
 

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