其他分享
首页 > 其他分享> > 剑指 Offer 10- II. 青蛙跳台阶问题

剑指 Offer 10- II. 青蛙跳台阶问题

作者:互联网

剑指 Offer 10- II. 青蛙跳台阶问题 - 力扣(LeetCode) (leetcode-cn.com)

运行结果

 代码

//提示:跳上n级台阶之前,要么在n-1级台阶,要么在n-2级台阶
class Solution {
public:
    int numWays(int n) {
        if(n <= 1) return 1;
        int M = 1e9 + 7;
        int Fn_2 = 1, Fn_1 = 1, Fn = 0;
        for(int i = 2; i <= n; ++i){
            Fn = (Fn_1 + Fn_2) % M;
            Fn_2 = Fn_1;
            Fn_1 = Fn;
        }
        return Fn;
    }
};

标签:10,台阶,Offer,int,青蛙,II
来源: https://blog.csdn.net/kku101xyz/article/details/122526007