Share my accepted Python solution 72ms


  • 0
    Z

    It's not elegant, but it works :P

    class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if nums == []:
            return 0
        counter = 1
        pointer = 1
        cur = nums[0]
        for i in range(1, len(nums)):
            if nums[i] == cur:
                counter += 1
                if counter <= 2:
                    nums[pointer] = cur
                    pointer += 1
            else:
                counter = 1
                cur = nums[i]
                nums[pointer] = cur
                pointer += 1
        return pointer

  • 0
    G

    Thanks for the solution. I didn't think in this direction.


Log in to reply
 

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