#include <iostream>
#include <vector>
using namespace std;
class Solution{
public:
int removeDuplicates(vector<int>& nums)
{
vector<int> temp;
int val;
bool key = false;
if(nums.size()<=2)
return nums.size();
int i=0;
while(i<nums.size())
{
if( i==0 || nums[i]!=nums[i-1] )
{
val = nums[i];
i++;
key = false;
continue;
}
if(nums[i]==val && !key)
{
key = true;
i++;
continue;
}
else nums.erase(nums.begin()+i);
}
for(auto i:nums)
cout<<i<<endl;
return nums.size();
}
};
int main() {
vector<int> test = {1,2,2,2,4,4};
Solution fir;
int num = fir.removeDuplicates(test);
cout<<num;
return 0;
}
LeetCode题目80.删除有序数组中的重复项Ⅱ(C++)
于 2023-09-11 16:45:07 首次发布