Python solution (linear time, and constant space) with explanation

  • 0
    # linear time, and constant space complexity
    def removeDuplicates(self, nums):
        if len(nums) == 0:
            return 0
        count = 1
        i = 0
        while i < len(nums)-1:
            if nums[i] == nums[i+1]:
                count += 1
                i += 1
        return count

    Explanation: If you encounter a duplicate between the number at the current index, and the number at the next index, then delete it. Otherwise, the numbers are not the same, so increment the count.

Log in to reply

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