Sword Finger Offer Interview Question 10-II. Frog Jumping Steps [Simple]-Dynamic Programming

My solution:

Note: You also need to consider the case of n = 0, because the first example is 0, which has been stuck for a long time.

Although this question is the same as 70 questions to climb stairs, but 70 questions can be passed without considering 0

class Solution {
public:
    int numWays(int n) {
        if(n==1||n==0)    return 1;
        if(n==2)    return 2;
        vector<int> dp(n);
        dp[0]=1;
        dp[1]=2;
        for(int i=2;i<n;i++){
            dp[i]=(dp[i-1]+dp[i-2])%1000000007;
        }
        return dp.back();
    }
};

Published 65 original articles · Like1 · Visits 488

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105461986
Recommended