Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 362a894

Browse files
Update 0322.零钱兑换.md
添加 python3 版本代码
1 parent 2c24d81 commit 362a894

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

‎problems/0322.零钱兑换.md‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
示例 5:
3636
输入:coins = [1], amount = 2
3737
输出:2
38-
38+
3939
提示:
4040

4141
* 1 <= coins.length <= 12
@@ -209,6 +209,36 @@ class Solution {
209209
210210
Python:
211211
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+
212242

213243
Go:
214244
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /