Skip to content

Commit

Permalink
Update 0509.斐波那契数.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghongcheng authored Jun 1, 2023
1 parent 69361bb commit 8e7d9f5
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions problems/0509.斐波那契数.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,41 @@ class Solution:
return dp[n]

```
动态规划(版本二)
态规划(版本二)
```python

class Solution:
def fib(self, n: int) -> int:
if n < 2:
if n <= 1:
return n
a, b, c = 0, 1, 0
for i in range(1, n):
c = a + b
a, b = b, c
return c

dp = [0, 1]

for i in range(2, n + 1):
total = dp[0] + dp[1]
dp[0] = dp[1]
dp[1] = total

return dp[1]


```
动态规划(版本三)
```python
class Solution:
def fib(self, n: int) -> int:
if n <= 1:
return n

prev1, prev2 = 0, 1

for _ in range(2, n + 1):
curr = prev1 + prev2
prev1, prev2 = prev2, curr

return prev2




```
Expand Down

0 comments on commit 8e7d9f5

Please sign in to comment.