【leetcode】70. 爬楼梯

70. 爬楼梯

/*具有重叠子问题,使用动态规划方法解决问题*/ 
class Solution {
public:
    int climbStairs(int n) {
        vector<int> memo(n+1, -1);
        memo[1] = 1;//只有一节台阶时,存在一种方法 
        memo[2] = 2;//有两节台阶时,存在两种方法 
        for(int i = 3; i <= n; i++)//有三节以上的台阶时,方法数为最近一步采取走两节或者走一节台阶的方法数总和 
            memo[i] = memo[i-1] + memo[i-2];
        return memo[n];
    }
};

猜你喜欢

转载自blog.csdn.net/qq_28038873/article/details/81670729