剑指offer 斐波那契数列

递推,空间复杂度O(1)。

class Solution {
public:
    int Fibonacci(int n) {
        if(n == 0)
            return 0;
        if(n == 1)
            return 1;
		int temp1, temp2, temp3;
        temp1 = 0;
        temp2 = 1;
        for(int i = 2; i <= n; i++)
        {
            temp3 = temp1 + temp2;
            temp1 = temp2;
            temp2 = temp3;
        }
        return temp3;
    }
};


你可能感兴趣的:(C++,算法,数据,剑指offer)