https://leetcode-cn.com/problems/climbing-stairs
斐波那契数列,注意要用递推
class Solution:
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1
if n == 2:
return 2
x1, x2 = 1, 2
while n >= 3:
tmp = x2
x2 = x1+x2
x1 = tmp
n -= 1
return x2
注意还有一种变态爬楼梯的问题,就是每一次可以爬1 2 3 … n阶楼梯,这其实简单写写推一下就好。