leetcode 剑指 Offer 10- I. 斐波那契数列

2020-11-16  本文已影响0人  Source_Chang

leetcode

C++:

class Solution {
public:
    int fib(int n) {

        if ( n < 2 ) {

            return n;
        }

        int prepre = 0;
        int pre = 1;
        int i = 2;
        while ( i < n ) {

            int temp = pre % 1000000007; 
            pre = ( prepre % 1000000007 + pre % 1000000007 ) % 1000000007;
            prepre = temp;
            
            ++i;
        }

        return ( prepre % 1000000007 + pre % 1000000007 ) % 1000000007;
    }
};
上一篇下一篇

猜你喜欢

热点阅读