题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
分类:数组
解法1:根据前两项计算第三项,然后更新前两项即可。
[java]
view plain
copy
public class Solution { public int Fibonacci(int n) { if(n==0) return 0; if(n==1||n==2) return 1; int pre = 1; int next = 1; int t = 0; int i =2; while(i<n){ t = pre+next; pre = next; next = t; i++; } return next; } }
原文链接 http://blog.csdn.net/crazy__chen/article/details/44986495