Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.面试
Note:数组
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?app
Example 1:spa
Input: [2,2,3,2] Output: 3
Example 2:code
Input: [0,1,0,1,0,1,99] Output: 99
去!你!妹!哦!blog
for x: nums ab = ab @ x
就能够啦~three
至于 @ 这个符号具体该怎么计算啊=。=要经过位运算实现啊(硬凑仍是能凑出来的。。。可意会不可言传。。。咳。。。element
完整代码:it
var singleNumber = function(nums) { let a = 0, b = 0; for (let x of nums) { [a, b] = [a ^ ((a | b) & x), (~a) & (b ^ x)]; } return b; };