80. Remove Duplicates from Sorted Array II
# Medium
Two pointers work together. One pointer points first element, the other one points third element.
Solution 1:
Consider edge cases: less or equal than 2 elements
Remove shouldn't be existed element
Set
p1=0, p2=2
nums[p1] == nums[p2]
indicates the element is repeated 3 times, thenpop
itnums[p1] != nums[p2]
, thenp1
andp2
just move forward one step
Solution 2(faster):
Consider edge cases: less or equal than 2 elements
Set count=1, and track last element x
Move should be existed element to correct position
Last updated
Was this helpful?