DP

LeetCode 70 Climbing Stairs (Python)

Posted by 小明MaxMing on July 31, 2020

题目

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

解题思路

dp, dp[i]=dp[i-1]+dp[i-2],因为到第n级台阶,可以由n-1或者n-2级台阶走到

代码

class Solution:
    def climbStairs(self, n: int) -> int:
        dp1, dp2 = 1, 2
        for i in range(n - 1):
            dp1, dp2 = dp2, dp1 + dp2
        return dp1

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅