-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMajorityElement2.cpp
55 lines (55 loc) · 1.32 KB
/
MajorityElement2.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
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> v;
int n1, n2, c1 = 0, c2 = 0;
if(nums.size() <= 1) return nums;
for(int i = 0; i < nums.size(); i++)
{
if(c1*c2 != 0)
{
if(nums[i] == n1)
c1++;
else if(nums[i] == n2)
c2++;
else
{
c1--, c2--;
}
}
else
{
if(c1 == 0 && nums[i] != n2)
{
n1 = nums[i];
c1++;
}
else if(c2 == 0 && nums[i] != n1)
{
n2 = nums[i];
c2++;
}
else
{
if(nums[i] == n1)
c1++;
else
c2++;
}
}
}
int c3 = 0, c4 = 0;
for(auto a: nums)
{
if(a == n1)
c3++;
if(a == n2)
c4++;
}
if(c3*3 > nums.size())
v.push_back(n1);
if(n2 != n1 && c4*3 > nums.size())
v.push_back(n2);
return v;
}
};