动态规划:
dp[i]
:表示到第i
阶有多少种爬法
dp[0] = 0
dp[1] = 1
:1;dp[2] = 2
:1+1;2;dp[3] = 3
:1 + 1 + 1 ‾ \underline{1} 1; 2 + 1 ‾ \underline{1} 1; 1 + 1 ‾ \underline{1} 1;
爬上 i−1 阶楼梯的方法数量。因为再爬1阶就能到第i阶
爬上 i−2 阶楼梯的方法数量,因为再爬2阶就能到第i阶
引用自:https://leetcode-cn.com/problems/climbing-stairs/solution/hua-jie-suan-fa-70-pa-lou-ti-by-guanpengchn/
class Solution {
public:
int climbStairs(int n) {
vector<int>dp(n+2, 0);
dp[0] = 0;
dp[1] = 1;
dp[2] = 2;
for(int i = 3; i <= n; i++)
{
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
};