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?
这个题目还是挺简单的,关键使用到了位或运算符 ^
int xor =0;
for(int i:nums){
xor^=i;
}
return xor;
这样性能应该是最好的。
突然做到这个题目又想了一下,因为自己对位运算符使用比较生疏,然后突然发现真的好用,仅仅四行代码就解决了,因此开发者要学会用更多灵活的方式解决问题。