Rejected by OJ, but got correct answer in my computer (Python)


  • 1
    Y
    def removeElement(A, elem):
        if not A:
            return 0
    
        lis = []
        for item in A:
            if (item != elem):
            	lis.append(item)
    
        A = lis
        
        #check what left in A
        #print A
        return len(A)
    
    print removeElement([4,5],4)
    

    I got [5], 1 on my computer, but OJ think my output is [4] . I don't know how this happen.


  • 0
    V

    I think the issue is not with the logic, but with extra space you used. In the question it was asked to do inplace, not using extra memory.

    Give a try removing 'lis' and check.


  • 0
    V

    Also, forgot to mention it seems that OJ is processing A with the number of elts you returned. As you haven't done inplace, A seems not changed in their main program where this function is called.


Log in to reply
 

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