136. Single Number
2020-10-17 本文已影响0人
jluemmmm
找到一个数组中只出现一次的元素,使用异或实现。
Runtime: 80 ms, faster than 79.41%
Memory Usage: 40.3 MB, less than 5.04%
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
return nums.reduce((a, b) => a ^ b)
};
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
let res = 0
let len = nums.length
for (let i = 0; i < len; i++) {
res ^= nums[i]
}
return res
};