Skip to content

[剑指 Offer] 10- II. 青蛙跳台阶问题 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
frdmu opened this issue Jul 23, 2021 · 0 comments
Open

[剑指 Offer] 10- II. 青蛙跳台阶问题 #35

frdmu opened this issue Jul 23, 2021 · 0 comments

Comments

@frdmu
Copy link
Owner

frdmu commented Jul 23, 2021

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

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

示例 1:

输入:n = 2
输出:2

示例 2:

输入:n = 7
输出:21

示例 3:

输入:n = 0
输出:1

提示:

  • 0 <= n <= 100




解法类似[剑指 Offer] 10- I. 斐波那契数列,只是初始值不同。代码如下:

class Solution {
public:
    int mod = 1e9 + 7;
    int numWays(int n) {
        if (n == 0)
            return 1;
        if (n == 1)
            return 1;
        int pre = 1;
        int cur = 1;
        int now;
        for (int i = 2; i <= n; i++) {
            now = (pre + now) % mod;
            pre = cur;
            cur = now;
        }    
        
        return now;
    }
};
@frdmu frdmu changed the title [剑指 Offer] 10- I. 青蛙跳台阶问题 [剑指 Offer] 10- II. 青蛙跳台阶问题 Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant