Merge pull request #387 from haofengsiji/update1

Update 0322.零钱兑换.md
This commit is contained in:
程序员Carl
2021-06-12 16:27:55 +08:00
committed by GitHub

View File

@ -35,7 +35,7 @@
示例 5 示例 5
输入coins = [1], amount = 2 输入coins = [1], amount = 2
输出2 输出2
 
提示: 提示:
* 1 <= coins.length <= 12 * 1 <= coins.length <= 12
@ -209,6 +209,36 @@ class Solution {
Python 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
```go ```go