Why my code is wrong?(by python)


  • 0
    E
        if(A == []):
            return 0
        A=[i for i in A if (i != elem)]
        return len(A)
    

    Input: [4,5], 4
    Output: [4]
    Expected: [5]


  • 2
    W

    Hi echoye:

    It seems that your code did nothing to the original list A. You created a new list named 'A' in line 3 in your code, and it only looks the same as what should return. Of course, the length is 1, which is correct, so the OJ cut the original input list at position 1 and gave WA.

    Maybe you can try to create a list to record the occurrences of 'elem' and use A.remove(elem) to pass OJ.


  • 0
    E

    Thank you very much!It seems that I don't understand the problems clearly.


  • 0
    L

    Same for me - had difficulty to understand the question. Thanks for the explanation. The Python solution is very simple:

    while elem in A:
    A.remove(elem)
    return len(A)


Log in to reply
 

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