Fib数列
题目链接:力扣Fib数列
递归(超时)
class Solution {
public int fib(int n) {
if(n==0)
return 0;
if(n==1||n==2)
return 1;
else
return fib(n-1)+fib(n-2);
}
}
递推
class Solution {
public int fib(int n) {
long f[] = new long[105];
f[1] = 1;
f[2] = 1;
for(int i = 3;i<=n;i++)
{
f[i] = (f[i-1]%(1000000007))+(f[i-2]%(1000000007));
}
return (int)(f[n]%(1000000007));
}
}