-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSingleNumberII.js
50 lines (45 loc) · 1013 Bytes
/
SingleNumberII.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Given an 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?
*
* Accepted.
*/
/**
* @param {number[]} nums
* @return {number}
*/
let singleNumber = function (nums) {
let one = 0;
let two = 0;
let three = 0;
nums.forEach(function (num) {
two |= (one & num);
one ^= num;
three = one & two;
two -= three;
one -= three;
});
return one
};
if (singleNumber([1]) === 1) {
console.log("pass")
} else {
console.error("failed")
}
if (singleNumber([1, 1, 1]) === 0) {
console.log("pass")
} else {
console.error("failed")
}
if (singleNumber([1, 1, 1, 2]) === 2) {
console.log("pass")
} else {
console.error("failed")
}
if (singleNumber([1, 1, 1, 2, 2, 2, 3]) === 3) {
console.log("pass")
} else {
console.error("failed")
}