写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项。斐波那契数列定义如下:
代码如下:
public class Solution { public int Fibonacci(int n) { if(n == 0) return 0; if(n == 1) return 1; int numfn1 = 0, numfn2 = 1; int currentnum=0; for(int i=2; i<=n; ++i) { currentnum = numfn1+numfn2; numfn1 = numfn2; numfn2 = currentnum; } return currentnum; } }