forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0169.cpp
More file actions
31 lines (29 loc) · 634 Bytes
/
0169.cpp
File metadata and controls
31 lines (29 loc) · 634 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int majorityElement(vector<int>& nums)
{
int cnt = 0, res = 0;
for (int num : nums)
{
if (cnt == 0)
{
res = num; ++cnt;
}
else if (num == res) ++cnt;
else --cnt;
}
return res;
}
};
int main()
{
vector<int> numbers = {2, 7, 11, 15, 2, 2, 2};
cout << Solution().majorityElement(numbers);
return 0;
}