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; };