Python code not accepted, where is the bug?


  • 0
    _

    My code is as follows,

    class Solution(object):
        def removeElement(self, nums, val):
            """
            :type nums: List[int]
            :type val: int
            :rtype: int
            """
            counter = 0
            index = 0
            new_len = 0
            
            while counter < len(nums):
                if nums[index] == val:
                    nums = nums[:index] + nums[index+1:] + [nums[index]]
                else:
                    index += 1
                    new_len += 1
                counter += 1
            return new_len
    

    The judging system says I have wrong answer for input [3,2,2,3], 3, but I got 2 (the true answer) in my editor. What has gone wrong?


Log in to reply
 

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