题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
/*
1.用数组设置前两个数值。
2.用循环从下向上计算。根据f(0)f(1)算出f(2),f(1)f(2)算出f(3),以此类推。
*/
class Solution {
public:
int Fibonacci(int n)
{
int result[2]={0,1};
if (n<2)
return result[n];
long long first = 0;
long long second = 1;
long long fibN = n;
for (int i =2;i<=n;i++)
{
fibN = first +second;
first = second;
second = fibN ;
}
return fibN ;
}
};
408

被折叠的 条评论
为什么被折叠?



