加个备忘录,不然会重复计算
public class Solution { // 0 1 1 2 3 5 8 int[] dp = new int[45]; public int Fibonacci(int n) { if(n == 0 || n == 1) return n; if(dp[n] != 0) return dp[n]; return dp[n] = Fibonacci(n-1)+Fibonacci(n-2); } }
加个备忘录,不然会重复计算
public class Solution { // 0 1 1 2 3 5 8 int[] dp = new int[45]; public int Fibonacci(int n) { if(n == 0 || n == 1) return n; if(dp[n] != 0) return dp[n]; return dp[n] = Fibonacci(n-1)+Fibonacci(n-2); } }