Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
40 lines (33 loc) · 997 Bytes

File metadata and controls

40 lines (33 loc) · 997 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* 剑指Offer,数组中只出现一次的数字
*/
public class FindTwoNumsAppearOnce {
public static void main(String[] args) {
int[] array = {2, 3, 4, 5, 2, 3, 4, 1};
int[] num1 = new int[1];
int[] num2 = new int[2];
FindNumsAppearOnce(array, num1, num2);
System.out.println(num1[0] + ", " + num2[0]);
}
public static void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
int mix = 0;
for (int i = 0; i < array.length; i++) {
mix = mix ^ array[i];
}
int mask = 1;
while ( (mask & mix) == 0) {
mask = mask << 1;
}
int result1 = 0;
int result2 = 0;
for (int i = 0; i < array.length; i++) {
if ( (mask & array[i]) == 0) {
result1 = result1 ^ array[i];
}else {
result2 = result2 ^ array[i];
}
}
num1[0] = result1;
num2[0] = result2;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.