What is wrong with my code? (Python)


  • 0
    def removeDuplicates(self, A):
        if len(A)==0 or len(A)==1:
            return len(A)
        i=1
        j=1
        while i<len(A):
            if A[i]!=A[i-1]:
                A[j]=A[i]
                j=j+1
            i=i+1
        A[j:]=[]
        return j
    

    Input: [1,1]

    Output: del, pop(), or remove() are not allowed.

    Expected: [1]

    I did not use del/pop/remove. I just use A[j: ] to shrink the list. Any one knows what's the problem?


  • 1

    You are not allowed to shrink the list. Instead you return the size of the list after duplicate items are "removed".

    See here if you are still confused what to return.


  • 0
    U

    1337c0d3r gave the answer. You are not required to shrink the list A, just return the correct size for unique numbers. The OJ refused to accept any size modification of A.


Log in to reply
 

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