如何理解利用按位异或来得到数组中不重复的项?
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
湛泸剑EX
10 years, 5 months ago