斐波那契问题解法
## 给定整数N,返回斐波那契数列的第N项
时间复杂度为2的N方解法
public int f1(int N){
if (n<1)
{
reutrn 0;
}
if(N==1 || N==2)
{
return 1;
}
return f1(N-1)+f1(N-2);
} 时间复杂度为N解法
public f2(int N){
if(N<1)
{
return 0;
}
if(N==1 || N==2)
{
return 1;
}
int res =1;
int pre = 1;
int temp = 0;
for(int i=3;i<=N;i++)
{
tem = res;
res = temp+pre;
pre = temp;
}
return res;
}
查看11道真题和解析