File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change 35
35
示例 5:
36
36
输入:coins = [ 1] , amount = 2
37
37
输出:2
38
-
38
+
39
39
提示:
40
40
41
41
* 1 <= coins.length <= 12
@@ -209,6 +209,36 @@ class Solution {
209
209
210
210
Python:
211
211
212
+ ```python3
213
+ class Solution:
214
+ def coinChange(self, coins: List[int], amount: int) -> int:
215
+ '''版本一'''
216
+ # 初始化
217
+ dp = [amount + 1]*(amount + 1)
218
+ dp[0] = 0
219
+ # 遍历物品
220
+ for coin in coins:
221
+ # 遍历背包
222
+ for j in range(coin, amount + 1):
223
+ dp[j] = min(dp[j], dp[j - coin] + 1)
224
+ return dp[amount] if dp[amount] < amount + 1 else -1
225
+
226
+ def coinChange1(self, coins: List[int], amount: int) -> int:
227
+ '''版本二'''
228
+ # 初始化
229
+ dp = [amount + 1]*(amount + 1)
230
+ dp[0] = 0
231
+ # 遍历物品
232
+ for j in range(1, amount + 1):
233
+ # 遍历背包
234
+ for coin in coins:
235
+ if j >= coin:
236
+ dp[j] = min(dp[j], dp[j - coin] + 1)
237
+ return dp[amount] if dp[amount] < amount + 1 else -1
238
+ ```
239
+
240
+
241
+
212
242
213
243
Go:
214
244
``` go
You can’t perform that action at this time.
0 commit comments