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

class Solution {
public:
    int fib(int n) {
        if (n == 0 || n == 1)
        {
            return n;
        }

        long long p = 1;
        long long q = 0;

        int i;
        for (i = 2;i <= n;i++)
        {
            long long temp = p;
            p = (p % 1000000007) + (q % 1000000007);
            q = temp;
        }

        return p % 1000000007;
    }
};

你可能感兴趣的:(算法)