面试题10- II. 青蛙跳台阶问题

一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。

答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。

示例 1:

输入:n = 2
输出:2
示例 2:

输入:n = 7
输出:21
提示:

0 <= n <= 100
注意:本题与主站 70 题相同:https://leetcode-cn.com/problems/climbing-stairs/

class Solution:
    def numWays(self, n: int) -> int:

        if n < 2:
            return 1
        if n == 2:
            return 2
        twoStepBefore = 1
        oneStepBefore = 2
        total = 0

        for i in range(n-2):
            total = twoStepBefore + oneStepBefore
            twoStepBefore = oneStepBefore
            oneStepBefore = total

        return total % 1000000007
        

 

posted @ 2020-03-31 18:48  米开朗菠萝  阅读(254)  评论(0编辑  收藏  举报