A very strange error with my Python code


  • 0
    Y

    Thi is very strange, here is my python code:
    When I run it on my own computer, it gives me the correct answer.
    When I run it in LeetCode, it gives me a different wrong answer.

    class Solution:
    # @param    A       a list of integers
    # @param    elem    an integer, value need to be removed
    # @return an integer
    def removeElement(self, A, elem):
        A = filter(lambda e: e!=elem, A)
        return len(A)

  • 0
    S

    By this means, you created a new variable A rather than the one given by input since it asked to remove element in place.
    You might try A[ : ] = filter(lambda e: e!=elem, A)


  • 0
    M

    so , I ran into a very similar problem in the string rotate exercise too . In both cases , my mistake was using the list as A , instead of A[:],

    Can someone explain whats the difference between A and A[:] ???


Log in to reply
 

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