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?
一个长度为n的梯子,一次只能走1,2步,多少种走法能到顶部?
思路如下,n-1到n有2种方法,n-1走一步,或者n-2走两步
f(n)=f(n-1)+f(n-2),著名的斐波那契数列
编程如下
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
a=1
b=1
for i in range(n):
a,b=b,a+b
return a