-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
137.cpp
40 lines (35 loc) · 1.05 KB
/
137.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
__________________________________________________________________________________________________
4ms
static int fast_io = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return 0;
}();
struct Solution {
int singleNumber(vector<int>& nums) {
int ones = 0, twos = 0;
for( auto num : nums){
ones = (ones ^ num) & ~twos;
twos = (twos ^ num) & ~ones;
}
return ones;
}
};
__________________________________________________________________________________________________
9432 kb
class Solution {
public:
int singleNumber(vector<int>& nums) {
vector<int> bits(32);
for (int n: nums) {
for (int i = 0; i < bits.size(); ++i) {
bits[i] += !!(n & (1<<i));
}
}
int x = 0;
for (int i = 0; i < bits.size(); ++i)
x += (!!(bits[i] % 3 != 0) << i);
return x;
}
};
__________________________________________________________________________________________________