力扣509. 斐波那契数 C++ 空间 O(1) 时间O(n)

int fib(int n) {
    if (n == 0)
        return 0;
    if (n == 1)
        return 1;

    int a = 0, b = 1, c;
    for (int i = 2; i <= n; ++i)
    {
        c = a + b;
        a = b;
        b = c;
    }

    return c;
}

你可能感兴趣的:(C++,LeetCode,c++,leetcode,开发语言)