366. Fibonacci【naive】

本文介绍了三种求解斐波那契数列的方法,包括递归、动态规划及空间优化策略。递归方法简单但效率低下;动态规划方法使用数组存储中间结果,虽占用较多空间但能有效解决问题;而空间优化方案仅需常数级空间即可实现。

Find the Nth number in Fibonacci sequence.

A Fibonacci sequence is defined as follow:

  • The first two numbers are 0 and 1.
  • The i th number is the sum of i-1 th number and i-2 th number.

The first ten numbers in Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

Notice

The Nth fibonacci number won't exceed the max value of signed 32-bit integer in the test cases.

 
Example

Given 1, return 0

Given 2, return 1

Given 10, return 34

 

解法一:

 1 class Solution {
 2     /**
 3      * @param n: an integer
 4      * @return an integer f(n)
 5      */
 6     public int fibonacci(int n) {
 7         if (n == 1 || n == 2) {
 8             return (n-1);
 9         }
10  
11         return fibonacci(n-1) + fibonacci(n-2);
12  
13     }
14 }

will lead to time limit

 

解法二:

 1 class Solution {
 2     public int fibonacci(int n) {
 3          // declare an array to store the result
 4          // it has to be n+2 to avoid out_of_bound
 5          int[] f = new int[n+2];
 6          f[1] = 0; // when input is 1 => zero
 7          f[2] = 1; // when input is 2 => 1
 8  
 9          int i = 3;
10          while (i <= n) { // it has to be incremental instead of decremental
11              f[i] = f[i-1] + f[i-2];
12              i++;
13          }
14  
15          return f[n];
16      }
17 }

will need O(n) space, using DP

 

解法三:

 1 class Solution {
 2     public int fibonacci(int n) {
 3         if (n < 3) {
 4             return n - 1;
 5         } 
 6         
 7         int first = 0;
 8         int second = 1;
 9         int third = 1;
10         
11         int i = 3;
12         while (i <= n) {
13             third = first + second;
14             first = second;
15             second = third;
16             i++;
17         }
18         return third;
19     }
20 }

will only need O(1) space,We can optimize the space used in method 2 by storing the previous two numbers only because that is all we need to get the next Fibannaci number in series

 

转载于:https://www.cnblogs.com/abc-begin/p/8157153.html

### 记忆化搜索求解斐波那契数列 记忆化搜索是一种结合递归和动态规划的技术,能够有效减少重复计算,提升算法效率。这种方法的核心在于利用一个辅助数组或字典保存已计算的结果,在后续递归调用中直接返回缓存值,而不是重新计算。 以下是基于 Python 实现的记忆化搜索方法: #### 方法描述 1. **定义函数参数**:创建一个带有额外参数 `memo` 的递归函数,用于存储已经计算过的斐波那契数值。 2. **初始条件判断**:如果当前索引对应于基础情况(如 \(F(0)\) 和 \(F(1)\)),则直接返回对应的值。 3. **查询缓存**:在每次递归前先检查目标值是否已经在 `memo` 中存在;若存在,则直接返回缓存中的结果。 4. **更新缓存**:当发现某个值尚未被计算时,按照斐波那契关系式进行计算,并将其加入到 `memo` 缓存中。 #### 代码实现 ```python def fibonacci(n, memo=None): if memo is None: memo = {} # 初始条件 if n == 0 or n == 1: return n # 查询缓存 if n in memo: return memo[n] # 更新缓存并返回结果 memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo) return memo[n] # 测试代码 print(fibonacci(10)) # 输出应为55 ``` 上述代码实现了记忆化搜索版本的斐波那契数列计算过程[^1]。通过引入 `memo` 字典作为外部存储器,避免了大量的冗余运算,使得时间复杂度降低至线性级别 O(N),空间复杂度同样为 O(N)[^5]。 #### 性能分析 相比于传统的纯递归方法,记忆化搜索极大地减少了不必要的重复操作。例如,在不采用任何优化措施的情况下,计算 Fibonacci 数列第 50 项可能需要数十亿次加法运算;而在应用记忆化策略之后,这一数量级骤降至仅仅几十次。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值