Non-negative Integers without Consecutive Ones LeetCode solution in JavaScript

1
var findIntegers = function (n) {
2
let dp = (len) => {
3
if (len < 0) return 0;
4
if (!len) return 1;
5
let _0x = 1; // number of accepted combination when '1' is first
6
let _1x = 1; // number of accepted combination when '0' is first
7
while (--len) [_0x, _1x] = [_0x + _1x, _0x];
8
return _0x + _1x;
9
};
10
let binary = n.toString(2);
11
let count = 0;
12
let is_prev_one = false;
13
for (let i = 0; i < binary.length; i++) {
14
if (binary[i] === "0") {
15
is_prev_one = false;
16
continue;
17
}
18
count += dp(binary.length - i - 1);
19
if (is_prev_one) return count;
20
is_prev_one = true;
21
}
22
return count + 1;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0