カエルが階段を跳ね上がる問題-2

リコウ住所

フィボナッチ数列:

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

 

おすすめ

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