-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0169_majorityElement.cc
63 lines (58 loc) · 1.16 KB
/
Problem_0169_majorityElement.cc
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
#include <vector>
#include "UnitTest.h"
using namespace std;
// 摩尔投票
class Solution
{
public:
int majorityElement(vector<int>& nums)
{
int hp = 0;
int candidate = 0;
for (auto& num : nums)
{
// 一次删掉两个不同的数
if (hp == 0)
{
candidate = num;
hp++;
}
else
{
hp += num == candidate ? 1 : -1;
}
}
// 剩下的数不一定是多数元素,需要检查
// {2, 3, 2, 3} 这种是不存在出现次数大于 n/2 的元素的
if (hp == 0)
{
// 没数剩下来
return -1;
}
// {1, 2, 3, 1, 2, 3, 1} 这种情况 hp > 0,但是 1 的出现次数显然没有大于 7/2 = 3 个
// 验证出现的次数
hp = 0;
for (auto& num : nums)
{
if (num == candidate)
{
hp++;
}
}
return hp > nums.size() / 2 ? candidate : -1;
}
};
void testMajorityElement()
{
Solution s;
vector<int> n1 = {3, 2, 3};
vector<int> n2 = {2, 2, 1, 1, 1, 2, 2};
EXPECT_EQ_INT(3, s.majorityElement(n1));
EXPECT_EQ_INT(2, s.majorityElement(n2));
EXPECT_SUMMARY;
}
int main()
{
testMajorityElement();
return 0;
}