Fibonacci sequence-1

Likou Address

Fibonacci sequence:

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

This question is slightly different from the initial value of Frog Jumping Step-2, it needs to be clear, the state transition equation is the same

class Solution {
public:
    int fib(int n) {
        if (n <= 1) return n;
        int first = 0;
        int second = 1;
        int thrid;
        for (int i = 2; i <= n; ++i) {
            thrid = (first + second) % 1000000007;
            first = second;
            second = thrid;
        }
        return thrid;
    }
};

 

Guess you like

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