mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
添加 0509.斐波那契数 Python3版本
This commit is contained in:
@ -187,7 +187,24 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
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:
|
||||||
|
if n < 2:
|
||||||
|
return n
|
||||||
|
return self.fib(n - 1) + self.fib(n - 2)
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user