-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
37 lines (37 loc) · 897 Bytes
/
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
class Solution {
public int[] findErrorNums(int[] nums) {
int res = 0;
for (int num : nums) {
res ^= num;
}
for (int i = 1, n = nums.length; i < n + 1; ++i) {
res ^= i;
}
int pos = 0;
while ((res & 1) == 0) {
res >>= 1;
++pos;
}
int a = 0, b = 0;
for (int num : nums) {
if (((num >> pos) & 1) == 0) {
a ^= num;
} else {
b ^= num;
}
}
for (int i = 1, n = nums.length; i < n + 1; ++i) {
if (((i >> pos) & 1) == 0) {
a ^= i;
} else {
b ^= i;
}
}
for (int num : nums) {
if (num == a) {
return new int[]{a, b};
}
}
return new int[]{b, a};
}
}