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