File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,12 @@ return dp[dp.length - 1];
117
117
118
118
## 代码
119
119
120
+
121
+ 代码支持:Python3,JavaScript:
122
+
123
+
124
+ JavaSCript Code:
125
+
120
126
``` js
121
127
/*
122
128
* @lc app=leetcode id=518 lang=javascript
@@ -146,10 +152,46 @@ var change = function(amount, coins) {
146
152
};
147
153
```
148
154
155
+ Python Code:
156
+
157
+ ```
158
+ class Solution:
159
+ def change(self, amount: int, coins: List[int]) -> int:
160
+ dp = [0] * (amount + 1)
161
+ dp[0] = 1
162
+
163
+ for j in range(len(coins)):
164
+ for i in range(1, amount + 1):
165
+ if i >= coins[j]:
166
+ dp[i] += dp[i - coins[j]]
167
+
168
+ return dp[-1]
169
+ ```
170
+
149
171
## 扩展
150
172
151
173
这是一道很简单描述的题目, 因此很多时候会被用到大公司的电面中。
152
174
153
175
相似问题:
154
176
155
177
[ 322.coin-change] ( ./322.coin-change.md )
178
+
179
+ ## 附录
180
+
181
+ Python 二维解法(不推荐):
182
+
183
+ ``` python
184
+ class Solution :
185
+ def change (self , amount : int , coins : List[int ]) -> int :
186
+ dp = [[0 for _ in range (len (coins) + 1 )] for _ in range (amount + 1 )]
187
+ for j in range (len (coins) + 1 ):
188
+ dp[0 ][j] = 1
189
+
190
+ for i in range (amount + 1 ):
191
+ for j in range (1 , len (coins) + 1 ):
192
+ if i >= coins[j - 1 ]:
193
+ dp[i][j] = dp[i - coins[j - 1 ]][j] + dp[i][j - 1 ]
194
+ else :
195
+ dp[i][j] = dp[i][j - 1 ]
196
+ return dp[- 1 ][- 1 ]
197
+ ```
You can’t perform that action at this time.
0 commit comments