剑指offer-2:斐波那契数列

二次信任 提交于 2019-11-27 07:57:21

二、斐波那契数列

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39

1.递归法

1). 分析 斐波那契数列的标准公式为:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=3,n∈N*) 根据公式可以直接写出:

2). 代码 

public class Solution {      public int Fibonacci(int n) {          if(n<=1){              return n;          }          return Fibonacci(n-1) + Fibonacci(n-2);      }  }

3). 复杂度

时间复杂度:O(2^n)

空间复杂度:O(1)

2. 优化递归

1). 分析

递归会重复计算大量相同数据,我们用个数组把结果存起来8!

2). 代码
public class Solution {      public int Fibonacci(int n) {          int ans[] = new int[40];          ans[0] = 0;          ans[1] = 1;          for(int i=2;i<=n;i++){              ans[i] = ans[i-1] + ans[i-2];          }          return ans[n];      }  }
3). 复杂度:

时间复杂度:O(n)

时间复杂度:O(n)

3. 优化存储

1). 分析

其实我们可以发现每次就用到了最近的两个数,所以我们可以只存储最近的两个数,sum 存储第 n 项的值, one 存储第 n-1 项的值, two 存储第 n-2 项的值。

2). 代码
public class Solution {      public int Fibonacci(int n) {          if(n == 0){              return 0;          }else if(n == 1){              return 1;          }          int sum = 0;          int two = 0;          int one = 1;          for(int i=2;i<=n;i++){              sum = two + one;              two = one;              one = sum;          }          return sum;      }  }
3). 复杂度:

时间复杂度:O(n)

时间复杂度:O(1)

4. 持续优化

1). 分析

观察上一版发现,sum 只在每次计算第 n 项的时候用一下,其实还可以利用 sum 存储第 n-1 项,例如当计算完 f(5) 时 sum 存储的是 f(5) 的值,当需要计算 f(6) 时,f(6) = f(5) - f(4),sum 存储的 f(5),f(4) 存储在 one 中,由 f(5)-f(3) 得到 如图: 

 

 

 

 

2). 代码
public class Solution {      public int Fibonacci(int n) {          if(n == 0){              return 0;          }else if(n == 1){              return 1;          }          int sum = 1;          int one = 0;          for(int i=2;i<=n;i++){              sum = sum + one;              one = sum - one;          }          return sum;      }  }
3). 复杂度

时间复杂度:O(n)

时间复杂度:O(1)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!