In-place solution in python for reference


  • 3
    B
    class Solution:
    # @param A a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        if len(A)==0:
            return 0
        cur=0
        point=0
        
        while point<len(A):
            if point<len(A)-2 and A[point]==A[point+1] and A[point]==A[point+2]:
                point=point+1
            else:
                A[cur]=A[point]
                point=point+1
                cur=cur+1
        
        
        return cur    
    

    this is essentially the same as problem I.only difference is to check if it has more than 3 duplicates


  • 0
    G

    This problem description obviously needs improvement. I don't even know A is checked before the fifth submission. My solution:

    class Solution:
    # @param A a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        num, cnt = 0, 0
        popout = []
        ret = 0
        for i in xrange(len(A)):
            if A[i] == num:
                if cnt < 2:
                    cnt += 1
                    ret += 1
                else:
                    popout.append(i)
            else:
                cnt = 1
                ret += 1
                num = A[i]
        for i in xrange(len(popout)):
            A.pop(popout[i] - i)
        return ret
    

  • 0
    H

    This could have been done in place with linear complexity.


Log in to reply
 

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