Skip to content

Latest commit

 

History

History
23 lines (22 loc) · 550 Bytes

80. Remove Duplicates from Sorted Array II.md

File metadata and controls

23 lines (22 loc) · 550 Bytes

80. Remove Duplicates from Sorted Array II (Medium)

[tag] two pointer.
It's a bit tricky.

  • use two pointers one fixed pointer(l) and one floating(r)
  • numbers on left of l is in order.
  • r will stay unless r found a different value
'''
    l 
1,1,2,2,2,2,2,2,3,2,3,4
    r
'''
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        l, r = 2, 2
        while r < len(nums):
            if nums[l-2] != nums[r]:
                nums[l] = nums[r]
                l += 1
            r += 1
        return l