forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_Bits_n.cpp
37 lines (32 loc) · 811 Bytes
/
AC_Bits_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_Bits_n.cpp
* Create Date: 2015-02-05 10:49:09
* Descripton: Count 32 bits.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int majorityElement(vector<int> &num) {
int bitcnt[32] = {0};
// swap the follow two 'for', it can work without bitcnt[]
for (auto &i : num) {
for (int j = 0; j < 32; ++j)
if (i & (1<<j))
++bitcnt[j];
}
int ans = 0;
for (int i = 0; i < 32; ++i)
if (bitcnt[i] > num.size() / 2)
ans |= (1<<i);
return ans;
}
};
int main() {
vector<int> num{-1, 1, 1};
Solution s;
cout << s.majorityElement(num) << endl;
return 0;
}