python LeetCode exam record 70

topic

Suppose you are climbing stairs. It takes n steps for you to reach the top of the building.

You can climb 1 or 2 steps at a time. How many different ways can you climb to the top of a building?

code

# 核心思想:将到达n层的次数转化为到达n-1层和n-2层的次数和
# 原因:因为一次只能走1步或者2步,所以到达该层只有两种方案,即n-1和n-2,然后计算这两层的方案数
class Solution:
    def climbStairs(self, n: int) -> int:
        ls = [i for i in range(1, n+1)]
        for i in range(2, n):
            ls[i] = ls[i-1]+ls[i-2]
        return ls[n-1]

Guess you like

Origin blog.csdn.net/weixin_46483785/article/details/132993671