var singleNumber = function(nums) {
for(var i = 1,len = nums.length;i<len;i++) {
nums[0] ^= nums[i]
}
return nums[0]
};
获取数组中只出现过一次的数字的算法,用到了XOR,但不太能理解
singleNumber([1,3,1,4,6,4,6,5,3])
[3, 3, 1, 4, 6, 4, 6, 5, 3]
[7, 3, 1, 4, 6, 4, 6, 5, 3]
[1, 3, 1, 4, 6, 4, 6, 5, 3]
[5, 3, 1, 4, 6, 4, 6, 5, 3]
[3, 3, 1, 4, 6, 4, 6, 5, 3]
[6, 3, 1, 4, 6, 4, 6, 5, 3]
[5, 3, 1, 4, 6, 4, 6, 5, 3]
只记得自身的XOR会得到0
添加回答
举报
0/150
提交
取消