フィボナッチ数列-1

リコウ住所

フィボナッチ数列:

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

この質問は、Frog Jumping Step-2の初期値とは少し異なります。明確に確認する必要があります。状態遷移式は、同じです。

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;
    }
};

 

おすすめ

転載: blog.csdn.net/TESE_yan/article/details/114199331