题目
![alt](https://img-blog.csdnimg.cn/img_convert/15edbabc1ea6d88e2c610d13081025a2.png)
题解
-
考察的是位运算 —— 异或(^),相同为 0,不同为 1 -
1^0 = 1,1^1 = 0 -
则直接对数据所有元素执行 ^ 操作,最终的就是结果
class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for (int num : nums) {
res = res ^ num;
}
return res;
}
}
本文由 mdnice 多平台发布