Concise Python Solution (Two Pointer)


  • 0
    S
    class Solution:
        # @param A a list of integers
        # @return an integer
        def removeDuplicates(self, A):
            fast = 0
            slow = 0
            while fast < len(A):
                A[slow] = A[fast]
                slow += 1
                # when duplicate happens, go through all duplicates with current number
                if fast+1 < len(A) and A[fast+1]==A[fast]:
                    A[slow] = A[fast+1]
                    slow += 1
                    i = 2
                    while fast+i < len(A) and A[fast+i] == A[fast]:
                        i += 1
                    fast += i
                else:
                    fast += 1
            return slow

Log in to reply
 

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