Given an array of integers, every element appears twice except for one. Find that single one.
public class Solution {
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
res ^= nums[i];
}
return res;
}
}
本文介绍了一种使用异或运算解决问题的方法,即在一个整数数组中,除了一个数字只出现一次外,其它数字都出现了两次,如何找出那个只出现一次的数字。
412

被折叠的 条评论
为什么被折叠?



