O(n) solution in Python


  • 0
    H
    class Solution(object):
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            i = 0
            while i < len(nums)-2:
                if nums[i]==nums[i+2]:  # because is sorted, this means nums[i],nums[i+1],nums[i+2] are equal
                    del nums[i+2]
                elif nums[i] == nums[i+1]:
                    i = i+2
                else:
                    i = i+1
            return len(nums)
    

Log in to reply
 

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