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

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

你可能感兴趣的:(leetcode 剑指 Offer 10- I. 斐波那契数列)