From 28cf557d7c7bcf208764e083e4550c411f7c2ba0 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 15:02:00 -0500 Subject: [PATCH] =?UTF-8?q?Update=200509.=E6=96=90=E6=B3=A2=E9=82=A3?= =?UTF-8?q?=E5=A5=91=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 08175058..195b68ac 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -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: