/
Solution.java
executable file
·48 lines (38 loc) · 1.16 KB
/
Solution.java
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
41
42
43
44
45
46
47
48
public class Solution {
public int singleNumber(int[] nums) {
int[] bits = new int[32];
for(int i = 0; i < 32; i++){
for(int n:nums){
if((n>>>i & 1) == 1) bits[i]++; // >> is also right
}
}
int res = 0;
for(int i = 0; i < 32; i++){
if(bits[i]%3 == 1) res |= 1<<i;
}
return res;
}
}
public class Solution {
public int singleNumber(int[] A) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int[] count = new int[Integer.SIZE];
int[] bit = new int[Integer.SIZE];
Arrays.fill(count, 0);
Arrays.fill(bit, 0);
for(int a : A){
for(int b = 0; b < Integer.SIZE; b++){
int x = a >>> b & 1;
bit[b] |= x;
if(x == 1)
count[b]++;
}
}
int s = 0;
for(int b = 0; b < Integer.SIZE; b++ ){
if (count[b] % 3 != 0)
s |= bit[b] << b;
}
return s;
}
}