nt singleNumber(int a[], int n) {
int num = 0;
for (int i = 0; i < n; ++i) {
num ^= a[i];
}
return num;
}
解释:What if the input is {1,1,2,2,0}? 就是两个一样的异或就是0,有一个不一样的异或出来就是那个值,这个太机智了!
the answer is still 0
0 ^ 1 = 1;
1^1=0;
0^2=2;
2^2=0;
0^0=0;
满足Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?