您的位置:首页 > 其它

爬梯子问题与斐波那契数列

2017-09-30 11:49 218 查看
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

        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: