用递归:
function Fibonacci(n){ if(n == 1 || n == 2){ return 1; }else{ return Fibonacci(n-1)+Fibonacci(n-2) } }
动态规划:
function fib(n){ if(n == 1 || n == 2) return 1; var dp = new Array(n+1); dp[1] = 1; dp[2] = 1; for(var i=3;i<=n;i++){ dp[i] = dp[i-1] + dp[i-2] } return dp[n] }