其他分享
首页 > 其他分享> > 力扣 题目80- 删除有序数组中的重复项 II

力扣 题目80- 删除有序数组中的重复项 II

作者:互联网

题目

题解

记录一下频率 超过2就删除

代码

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 class Solution {
 5 public:
 6     int removeDuplicates(vector<int>& nums) {
 7         int frequency = 1;
 8         for (int i = 0; i < nums.size(); i++) {
 9             if (i==0||nums[i] != nums[i-1]) {
10                 frequency = 1;
11             }
12             else
13             {
14                 frequency += 1;
15             }
16             if (frequency > 2) {
17                 nums.erase(nums.begin() + i);
18                 i -= 1;
19                 frequency -= 1;
20             }
21         }
22         return nums.size();
23     }
24 };
25 int main() {
26     Solution sol;
27     vector<int> nums = { 0,0,1,1,1,1,2,3,3 };
28     int result=sol.removeDuplicates(nums);
29     for (int i = 0; i < result; i++) {
30         cout << nums[i] << " ";
31     }
32 }
View Code

 

标签:力扣,nums,int,sol,II,frequency,result,80,removeDuplicates
来源: https://www.cnblogs.com/zx469321142/p/16439773.html