leetcode-python-爬楼梯

1--1种

2--2种

3--3种

4--5种

5--8种

…………

写一写发现类似斐波那契数列,后一项等于前两项之和

class Solution:
    def climbStairs(self, n: int) -> int:
        if n <= 2:
            return n
        a = 1
        b = 2
        while n > 2:
            a,b = b,a+b
            n -= 1
        return b

或者递归,但是容易超时

class Solution:
    def climbStairs(self, n: int) -> int:
        if n <= 3:
            return n

        while n > 3:
            return self.climbStairs(n-2)+self.climbStairs(n-1)
            

 

posted @ 2021-06-07 14:47  泊鸽  阅读(88)  评论(0)    收藏  举报