LeetCode Sword Finger Offer 10- II. The problem of frog jumping

Original title link
ideas:

  1. The idea of ​​Fibonacci sequence, f(n) = f(n-1) + f(n-2).
  2. Note that the starting condition is f(0) = 1, f(1) = 1;
    see the hot comment explanation, hehe
    code:
class Solution {
    
    
public:
    int numWays(int n) {
    
    
        if(n ==1 || n == 0) return 1;
        int a = 1, b = 1, i = 0;
        int sum = 0;
        while(i <= n-2){
    
    
            sum =  (a + b) % 1000000007;
            b = a;
            a = sum;
            i++;
        }
        return sum;
    }
};

Guess you like

Origin blog.csdn.net/qq_43078427/article/details/109954709