mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Update 0509.斐波那契数.md
This commit is contained in:
@ -203,18 +203,9 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
动态规划(版本一)
|
||||||
```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:
|
class Solution:
|
||||||
def fib(self, n: int) -> int:
|
def fib(self, n: int) -> int:
|
||||||
|
|
||||||
@ -238,7 +229,24 @@ class Solution:
|
|||||||
# 返回答案
|
# 返回答案
|
||||||
return dp[n]
|
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:
|
class Solution:
|
||||||
def fib(self, n: int) -> int:
|
def fib(self, n: int) -> int:
|
||||||
if n < 2:
|
if n < 2:
|
||||||
|
Reference in New Issue
Block a user