diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 8be11312..112c96f9 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -187,7 +187,24 @@ class Solution { ``` 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: