← 返回所有文章

剑指offer(斐波那契数列)

  • 避免递归造成的调用栈消耗
public class Solution {
    public int Fibonacci(int n) {
        int a = 0;
        int b = 1;
        while(n-->0){
            b = a + b;
            a = b - a;
        }
        return a;
    }
}
  • 复杂度
    时间复杂度:O(n)O(n)
    空间复杂度:O(1)O(1)

搜索文章

搜索这些年写下的文字。

输入关键词搜索 · Esc 关闭