leetcode六月每日一題 leetcode70

在這裏插入圖片描述
這是一道基礎的動態規劃題目

class Solution {

private:

    vector<int> memo;

    int calWays( int n ){

        if( n == 0 || n == 1 )
            return 1;

        if( memo[n] == -1 )
            memo[n] = calWays(n-1) + calWays(n-2);
            //可以由前一個跳過去,可以由前兩個跳過去

        return memo[n];
    }
public:
    int climbStairs(int n) {

        memo = vector<int>(n+1,-1);
        return calWays(n);

    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章