真是有点意思,让我想我真想不出来。
public class Solution {
public int[] singleNumber(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++)
sum ^= nums[i];
int i = 1;
while ((sum & i) == 0) {
i <<= 1;
}
int n = 0;
for (int num: nums) {
if ((num & i) != 0)
n++;
}
int[] result1 = new int[n];
int tmp = 0;
for (int j = 0; j < nums.length; j++) {
if ((nums[j] & i) != 0)
result1[tmp++] = nums[j];
}
int r1 = 0, r2 = 0;
for (int j = 0; j < result1.length; j++)
r1 ^= result1[j];
r2 ^= r1;
for (int j = 0; j < nums.length; j++)
r2 ^= nums[j];
int[] result = {r1, r2};
return result;
}
}