// class Solution {
// public int fib(int n) {
// if(n==0||n==1){
// return n;
// }
// // 重复算了很多遍,时间复杂度炸了
// return (fib(n-1)+fib(n-2));
// }
// }
// 备忘录
class Solution {
int[] help;
public int fib(int n) {
help=new int[n+1];
return hfib(n);
}
public int hfib(int n){
if(n==0||n==1){
return n;
}
if(help[n]!=0){
return help[n];
}
help[n]=(hfib(n-1)+hfib(n-2))%1000000007;
return help[n];
}
}
// 动态规划
class Solution {
public int fib(int n) {
if(n==0||n==1){
return n;
}
int[] res=new int[n+1];
res[0]=0;
res[1]=1;
for(int i=2;i<n+1;i++){
// 每一步都模
res[i]=(res[i-1]+res[i-2])%1000000007;
}
return res[n];
}
}