思路:
- 两个指针,比Remove Duplicates from Sorted Array多了一个count,记录是否超过两次。直接贴代码。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() <= 2) return nums.size();
int pre = 0,count = 1;
for(int i = 0; i < nums.size()-1; i++){
if(nums[i] == nums[i+1]){
if(count < 2){
count++;
pre++;
nums[pre] = nums[i];
}
}else if(nums[i] != nums[i+1]){
count = 1;
pre++;
nums[pre] = nums[i+1];
}
}
return pre+1;
}
};