80. Remove Duplicates from Sorted Array II (Medium) Facebook
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted arraynums=[1,1,1,2,2,3]
,
Your function should return length =5
, with the first five elements of nums being1
,1
,2
,2
and3
. It doesn't matter what you leave beyond the new length.
Solution 1: 同向Two Pointers O(n); O(1) Template
/**
* Template 同向Two Pointers O(n);O(1)
* Duplicates are allowed at most twice.
* It means that the number can be the same as the last number.
* Instead of comparing with the last number, compare with the second last.
* Implementation:
* Edge case: If nums.length <= 2, already true, return nums.length.
* For each number n in nums:
* | If len < 2 or n > nums[len - 2]:
* | Set nums[len] to n. Add len by 1.
* Return len.
*/
public int removeDuplicates(int[] nums) {
if (nums == null) {
return -1;
}
int len = 0;
for (int num : nums) {
if (len < 2|| num > nums[len - 2]) {
nums[len++] = num;
}
}
return len;
}