From 362a8947f04cbca1ceed2ad2dac392dd95ca81fb Mon Sep 17 00:00:00 2001 From: haofeng <852172305@qq.com> Date: Sat, 12 Jun 2021 11:08:35 +0800 Subject: [PATCH] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2.md=20=E6=B7=BB=E5=8A=A0=20python3=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0322.零钱兑换.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index cc288593..cf537088 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -35,7 +35,7 @@ 示例 5: 输入:coins = [1], amount = 2 输出:2 -  + 提示: * 1 <= coins.length <= 12 @@ -209,6 +209,36 @@ class Solution { Python: +```python3 +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + '''版本一''' + # 初始化 + dp = [amount + 1]*(amount + 1) + dp[0] = 0 + # 遍历物品 + for coin in coins: + # 遍历背包 + for j in range(coin, amount + 1): + dp[j] = min(dp[j], dp[j - coin] + 1) + return dp[amount] if dp[amount] < amount + 1 else -1 + + def coinChange1(self, coins: List[int], amount: int) -> int: + '''版本二''' + # 初始化 + dp = [amount + 1]*(amount + 1) + dp[0] = 0 + # 遍历物品 + for j in range(1, amount + 1): + # 遍历背包 + for coin in coins: + if j >= coin: + dp[j] = min(dp[j], dp[j - coin] + 1) + return dp[amount] if dp[amount] < amount + 1 else -1 +``` + + + Go: ```go