题目描述
你在爬楼梯,需要n步才能爬到楼梯顶部
每次你只能向上爬1步或者2步。有多少种方法可以爬到楼梯顶部?
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?
解答:
和剑指offer这道题一样,变相的斐波那契数列。
class Solution:
def climbStairs(self , n ):
# write code here
result = [0, 1, 2]
for i in range(3, n+1):
result.append(result[-1]+result[-2])
return result[n]
本文探讨了一个经典的数学问题——爬楼梯。通过分析题目,我们发现该问题实际上是一个变相的斐波那契数列问题。文章提供了一种使用Python编程语言解决该问题的方法,即通过构建一个动态数组来存储每个台阶到达的方法数量,从而求解出爬到楼梯顶部的不同方式总数。
&spm=1001.2101.3001.5002&articleId=107706798&d=1&t=3&u=6fc1f5bfe8fe42fc9cd0afdc459facf8)
235

被折叠的 条评论
为什么被折叠?



