Fast Fibonacci recursion

前端 未结 9 1694

I\'m trying to recall an algorithm on Fibonacci recursion. The following:

public int fibonacci(int n)  {
  if(n == 0)
    return 0;
  else if(n == 1)
    ret         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 23:56

    You need to memorize the calculated value in order to stop exponential growth.

    1. Just use an array to store the value.
    2. Check the array if you have already calculate it.
    3. If it finds it,use it or otherwise calculate it and store it.

    Here is an working example for faster recursion using memory.

    Calculating fibonacci number

提交回复
热议问题