Easy to understand Python solution O(N) time and O(1) space


  • 0
    J
    class Solution(object):
        def removeDuplicates(self, nums):
            counter = 0
            prev = None
            
            #keep track of how many you previously removed from List
            removed = 0
            
            for n in range(len(nums)):
                curr = nums[n-removed]
                if prev != curr:
                    counter = 1
                    prev = curr
                else:
                    counter += 1
                    prev = curr
              
                if counter > 2:
                    removed += 1
                    nums.remove(curr)
            return len(nums)
    
    

Log in to reply
 

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