LeetCode 80 Remove Duplicates from Sorted Array II
概述
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
双指针
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int last = INT_MIN;
int lastCount = 1;
int idx = 0;
for (int i = 0; i < nums.size(); i ++) {
if (nums[i] != last) {
last = nums[i];
lastCount = 1;
nums[idx++] = nums[i];
} else {
lastCount ++;
if (lastCount <= 2) {
nums[idx++] = nums[i];
}
}
}
return idx;
}
};