Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
找出唯一个只出现过一次的数字
要求线性时间复杂度 不使用额外空间
解法是用异或运算符来完成
a xor a = 0
所有的数字进行异或操作,结果就是单独的那个数
这个让我想起了storm 中的ack机制中,就是通过这种方法来确保信息的完整运输
public int singleNumber(int[] nums){
int result = 0;
for(int i : nums){
result ^= i;
}
return result;
}