moonlightshadow123
4/10/2017 - 3:07 PM

80-Remove-Duplicates-from-Sorted-Array-II <------> leetcode-4

80-Remove-Duplicates-from-Sorted-Array-II <------> leetcode-4

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

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?

For example, Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.