-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ137_SingleNumberII.java
More file actions
32 lines (29 loc) · 912 Bytes
/
Copy pathQ137_SingleNumberII.java
File metadata and controls
32 lines (29 loc) · 912 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
package leetcode;
public class Q137_SingleNumberII {
public static int singleNumber(int[] nums) { //自己模拟三进制运算
int[] w = new int[Integer.SIZE];
for(int i=0; i<nums.length; i++){
for(int j = 0; j < Integer.SIZE; j++){
w[j] = (w[j] + ((nums[i] >> j) & 1)) % 3;
}
}
int ans = 0;
for(int i=0; i<Integer.SIZE; i++)
ans += w[i] << i;
return ans;
}
public static int singleNumber2(int[] nums){
int one = 0, two = 0, three = 0;
for(int i=0; i<nums.length; i++){
two |= (one & nums[i]);//two位:11才进位1
one ^= nums[i]; //one位:第一层异或
three = ~(one & two); //three 看不懂
one &= three;
two &= three;
}
return one;
}
public static void main(String[] args) {
System.out.println(singleNumber(new int[]{2,2,2,3}));
}
}