-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM52.java
More file actions
30 lines (29 loc) · 816 Bytes
/
BM52.java
File metadata and controls
30 lines (29 loc) · 816 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
package NiukeTOP101;
public class BM52 {
public int[] FindNumsAppearOnce (int[] array) {
if(array.length <= 1){
return new int[]{};
}
int res1 = 0, res2 = 0, temp = array[0];
for (int i = 1; i < array.length; i++) {
temp ^= array[i];
}
int k = 1;
// 找到两个数不相同的第一位
while ((k & temp) == 0){
k <<= 1;
}
for(int i = 0; i < array.length; i++){
//遍历数组,对每个数分类
if((k & array[i]) == 0)
res1 ^= array[i];
else
res2 ^= array[i];
}
//整理次序
if(res1 < res2)
return new int[] {res1, res2};
else
return new int[] {res2, res1};
}
}