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