pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.39k stars 242 forks source link

青蛙跳台阶问题 #1044

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago

剑指 Offer 10- II. 青蛙跳台阶问题

/**
 * @param {number} n
 * @return {number}
 */
var sum = [];
var numWays = function(n) {
    if(n == 1 || n == 0) return 1;
    if(n == 2) return 2;
    if(sum[n])
        return sum[n];
    sum[n] = (numWays(n-2) + numWays(n-1)) % 1000000007;
    return sum[n];
};