The goodness of algorithm generally is defined by some factors like how fast it is, and how easy it is to understand.
Let me share the easiest code for calculating fibonaci:
private int fibonaci(int n) {
if (n < 3) return 1;
return fibonaci(n-1) + fibonaci(n-2);
}
But this is exponential with O(2^n) time complexity. Your algorithm has O(n) time complexity which is much much better than exponential.
There is even better algorithm to calculate n-th fibonaci number though and it has O(logn) time complexity. You can find it here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…