260. 只出现一次的数字 III - 力扣(LeetCode)
异或运算很奇妙
class Solution {
public int[] singleNumber(int[] nums) {
int eor1 = 0;
for (int i = 0; i < nums.length; i++) {
eor1 ^= nums[i];
}
int eor2 = 0;
int rightOne = eor1 & (-eor1);
for (int num : nums) {
if ((num & rightOne) == 0) {
eor2 ^= num;
}
}
return new int[] { eor2, eor1 ^ eor2 };
}
}