javascript solution using recursion


  • 0

    If using normal recursion it doesn't go through after n=35, so I store the calculate result so that I can avoid same calculation.

    var store = {}
    var count = 0;
    var climbStairs = function(n) { 
        count ++;
        var value;
        if (n in store) {
            value = store[n];
        } else {
            if (n === 0 || n === 1 || n === 2) {
                value = n;
            } else {
                value = climbStairs(n - 1) + climbStairs(n - 2);
            }
            store[n] = value;
        }
        return value;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.