Update 0509.斐波那契数.md

This commit is contained in:
jianghongcheng
2023-06-01 15:02:00 -05:00
committed by GitHub
parent ed4543b300
commit 28cf557d7c

View File

@ -203,18 +203,9 @@ class Solution {
```
### Python
动态规划(版本一)
```python
class Solution:
def fib(self, n: int) -> int:
if n < 2:
return n
a, b, c = 0, 1, 0
for i in range(1, n):
c = a + b
a, b = b, c
return c
# 动态规划 (注释版。无修饰)
class Solution:
def fib(self, n: int) -> int:
@ -238,7 +229,24 @@ class Solution:
# 返回答案
return dp[n]
# 递归实现
```
动态规划(版本二)
```python
class Solution:
def fib(self, n: int) -> int:
if n < 2:
return n
a, b, c = 0, 1, 0
for i in range(1, n):
c = a + b
a, b = b, c
return c
```
递归(版本一)
```python
class Solution:
def fib(self, n: int) -> int:
if n < 2: