509. 斐波那契数

老题目了,使用迭代可解(不建议用递归,会产生较多冗余计算)

时间O(n),空间O(1)

    public int fib(int n) {
        if (n<2) return n;
        int i=0,j=1,count=2;
        while(count<=n){
            int temp = j;
            j=i+j;
            i=temp;
            count++;
        }
        return j;
    }
争取早日不再是一只菜鸡
原文地址:https://www.cnblogs.com/jchen104/p/14656704.html