-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path169.majority_element_moore_voting.cpp
63 lines (53 loc) · 1.52 KB
/
169.majority_element_moore_voting.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
/*
* Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
*
* You may assume that the array is non-empty and the majority element always exist in the array.
*
* Example 1:
*
* Input: [3,2,3]
* Output: 3
* Example 2:
*
* Input: [2,2,1,1,1,2,2]
* Output: 2
*/
/**
* 思路1:
* 使用map来记录每一个数字出现的次数,空间和时间复杂度都是O(n)
*
* 思路2:
* 使用摩尔投票法(moore voting)的方式:
*
* 先假设第一个数是过半的元素,同时设置一个计数器置1
* 依次往下遍历,如果遇到和假定元素相同的值,则计数器+1。如果遇到和假定元素不相同的值,则计数器-1
* 当计数器为0时,说明与假定值不一样的元素个数已经比假定值个数多了,重新选定下一个元素作为假定值
* 当遍历完成,假定值就是最后结果
*/
#include <vector>
using namespace std;
class Solution {
public:
int majorityElement(vector<int> &nums) {
int ct = 0, ret = nums[0];
for (auto num : nums) {
if (ct == 0) {
ret = num;
}
if (ret == num) {
ct++;
} else {
ct--;
}
}
return ret;
}
};
int main() {
Solution s;
vector<int> n1 = {3, 2, 3};
assert(s.majorityElement(n1) == 3);
vector<int> n2 = {2, 2, 1, 1, 1, 2, 2};
assert(s.majorityElement(n2) == 2);
return 0;
}