mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -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
|
||||||
|
Reference in New Issue
Block a user