Frog jumping up the stairs problem-2

Likou Address

Fibonacci sequence:

f\, n = f\, n-1 + f\, n-2

class Solution {
public:
    int numWays(int n) {
        if (0 == n || 1 == n) return 1;
        if (2 == n) return 2;
        int first = 1, second = 2;
        int third;
        for (int i = 3; i <=n; ++i) {
            third = (first + second) % 1000000007;
            first = second;
            second = third;
        }
        return third;
    }
};

 

Guess you like

Origin blog.csdn.net/TESE_yan/article/details/114199068