169. 多数元素
为保证权益,题目请参考 169. 多数元素(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int majorityElement(vector<int> &nums) {
int candi = 0;
int count = 0;
for (auto num:nums) {
if (count == 0) {
candi = num;
count = 1;
} else {
if (candi == num) {
count += 1;
} else {
count -= 1;
}
}
}
return candi;
}
};
int main() {
return 0;
}
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
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