LeetCode(力扣)70. 爬楼梯Python

LeetCode70. 爬楼梯

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/climbing-stairs/
LeetCode(力扣)70. 爬楼梯Python_第1张图片

代码

class Solution:
    def climbStairs(self, n: int) -> int:
        if n <= 1:
            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]
        return dp[n]

你可能感兴趣的:(leetcode,python,算法,职场和发展)