Remove Duplicates from Sorted Array II

Problem: Remove Duplicates from Sorted Array II

All we need to do is find duplicates more than twice. Although the returned result of function is length, the sorted array still need to be modified in place to pass the OJ.

Code in Python:

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        i = 0
        while i < len(nums)-1:
            if nums[i+1] != nums[i]: i += 1
            else:
                j = i+1
                while j < len(nums)-1 and nums[j+1] == nums[j]:
                    nums.pop(j+1)
                i = j+1
        return len(nums)

results matching ""

    No results matching ""