forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_bitwise_n.cpp
More file actions
40 lines (35 loc) · 838 Bytes
/
Copy pathAC_bitwise_n.cpp
File metadata and controls
40 lines (35 loc) · 838 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
32
33
34
35
36
37
38
39
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_bitwise_n.cpp
* Create Date: 2015-01-06 10:37:07
* Descripton: bitwise!!!
* x[j] = (x[j-1] & a) | (x[j] & ~a)
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int singleNumber(int A[], int n) {
if (!n)
return 0;
int x0 = ~0, x1 = 0, x2 = 0, t;
for (int i = 0; i < n; i++) {
t = x2;
x2 = (x1 & A[i]) | (x2 & ~A[i]);
x1 = (x0 & A[i]) | (x1 & ~A[i]);
x0 = (t & A[i]) | (x0 & ~A[i]);
}
return x1;
}
};
int main() {
int n, a[100];
Solution s;
while (cin >> n) {
for (int i = 0; i < n; i++)
cin >> a[i];
cout << s.singleNumber(a, n) << endl;
}
return 0;
}