Day 91
题目描述
## 思路
交换律:a ^ b ^ c <=> a ^ c ^ b
任何数于0异或为任何数 0 ^ n => n
相同的数异或为0: n ^ n => 0
根据以上 很容易想到做法,将数组中所有的数异或起来,得到的就是只出现一次的数
java
class Solution {
public int singleNumber(int[] nums) {
int x=nums[0];
if(nums.length==0){
return x;
}
for(int i=1;i<nums.length;i++){
x=x^nums[i];
}
return x;
}
}