-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution260.java
More file actions
41 lines (34 loc) · 843 Bytes
/
Solution260.java
File metadata and controls
41 lines (34 loc) · 843 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
40
41
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 只出现一次的数字 III
* @date: 2019/01/30
*/
public class Solution260 {
public static void main(String[] args) {
int[] nums = {2, 1, 2, 3, 4, 1};
for (int e : new Solution260().singleNumber(nums)) {
System.out.print(e + " ");
}
}
public int[] singleNumber(int[] nums) {
int[] ans = new int[2];
if (null == nums || 0 >= nums.length) {
return ans;
}
int diff = 0;
for (int e : nums) {
diff ^= e;
}
// 取最右侧1位的1
diff &= -diff;
for (int e : nums) {
if (0 == (e & diff)) {
ans[0] ^= e;
} else {
ans[1] ^= e;
}
}
return ans;
}
}