Promble
link: https://leetcode.com/problems/climbing-stairs/
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
- 1 <= n <= 45
 
Thought
- dp有n+1个位置, dp中记录爬到当前台阶有几种爬的方法。
 - 状态迁移方程
dp[i] = dp[i-1] + dp[i-2]当前台阶的爬法 = 前一台阶的爬法+前两台阶的爬法。 因为一次可以爬一个或者两个台阶。所以当前台阶的爬法是相加的。 - 初始状态
dp[1] = 1, dp[2] = 2因为第一层台阶只有一种爬法,第二层台阶有两种爬法:- 1 step + 1 step
 - 2 steps
 
 - 循环从底三层台阶开始,因为前两层无法使用状态迁移方程。
 
Code
class Solution:
    def climbStairs(self, n: int) -> int:
        if n <=2: return n 
        dp = [0] * (n+1)
        dp[1]=1
        dp[2]=2
        for i in range(3, n+1):
            dp[i] = dp[i-1] + dp[i-2]
        print(dp)
        return dp[n]