-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path删除排序数组中的重复项 II.cpp
80 lines (68 loc) · 1.3 KB
/
删除排序数组中的重复项 II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <functional>
using namespace std;
/*
第一种思路利用count来统计连续出现次数,当nums[right] == nums[right - 1],判断count是否小于2
当nums[right] != nums[right - 1],count归零
第二种思路比较nums[i]是否与nums[k - 2],当不相等的时候,进行赋值
*/
class Solution
{
public:
int removeDuplicates(vector<int>& nums)
{
if (nums.size() < 3)
return nums.size();
int left = 1, right = 1;
int count = 1;
while (right < nums.size())
{
if (nums[right] == nums[right - 1])
{
if (count < 2)
{
++count;
nums[left++] = nums[right];
}
}
else
{
count = 1;
nums[left++] = nums[right];
}
++right;
}
return left;
}
int removeDuplicates2(vector<int>& nums)
{
if (nums.size() < 3)
return nums.size();
int k = 2;
for (int i = 2; i < nums.size(); ++i)
{
if (nums[i] != nums[k - 2])
nums[k++] = nums[i];
}
return k;
}
};
int main()
{
Solution a;
vector<int> nums{ 0,0,1,1,1,1,2,3,3 };
int b = a.removeDuplicates(nums);
cout << b << endl;
for (int i = 0; i < b; ++i)
cout << nums[i] << " ";
cout << endl;
system("pause");
return 0;
}