题解 | #斐波那契数列#

斐波那契数列

https://www.nowcoder.com/practice/ee5d403c1172487f8c7915b3c3d924c6

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

memo = {}
function fib(n) {
    if (n == 1 || n == 2) return 1;
    if (memo[n]) return memo[n]

    res = fib(n - 1) + fib(n - 2);
    memo[n] = res;
    return res;
}

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        console.log(fib(line));
    }
})();

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务