题目

题解
- 考察的是位运算 —— 异或(^),相同为 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;
}
}
隐私提示
昵称、网址和评论内容会公开显示,邮箱不公开。请勿在评论中填写敏感信息。