From 61838527a072acd7cf7fb4e3fce8de0f4ff91fce Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 10:07:34 -0400 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 增加java非压缩状态版本,易于理解 --- problems/0509.斐波那契数.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 4102dd64..cb54a0f9 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -186,6 +186,26 @@ class Solution { } ``` +```java +//非压缩状态的版本 +class Solution { + public int fib(int n) { + if (n <= 1) return n; + + int[] dp = new int[n + 1]; + + dp[0] = 0; + dp[1] = 1; + + for (int index = 2; index <= n; index++){ + dp[index] = dp[index - 1] + dp[index - 2]; + } + + return dp[n]; + } +} +``` + Python: ```python3 class Solution: