1、递归
{
// write code here
if(n <= 2) return 1;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
module.exports = {
Fibonacci : Fibonacci
};
2、动态规划
{
// write code here
// if(n <= 2) return 1;
// return Fibonacci(n - 1) + Fibonacci(n - 2);
let a = 1,b = 1,c = 1;
for(var i = 3;i <= n;i++){
c = a + b;
a = b;
b = c;
}
return c;
}
module.exports = {
Fibonacci : Fibonacci
};