Tuesday, September 9, 2014

Leetcode: Remove Duplicates from Sorted Array II @Python

class Solution:
    # @param A a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        if len(A)<=2:return len(A)
        slow=0
        cntr=1
        for fast in range(1,len(A)):
            if A[fast]==A[fast-1]:
                cntr+=1
                if cntr<=2:
                    slow+=1
                    A[slow]=A[fast]
            else:
                cntr=1
                slow+=1
                A[slow]=A[fast]
        return slow+1

No comments :

Post a Comment