Update 0518.零钱兑换II.md 补充二维dp数组的Java代码
This commit is contained in:
parent
f51773db0d
commit
334f4a27de
|
|
@ -215,6 +215,27 @@ class Solution {
|
|||
}
|
||||
}
|
||||
```
|
||||
```Java
|
||||
// 二维dp数组版本,方便理解
|
||||
class Solution {
|
||||
public int change(int amount, int[] coins) {
|
||||
int[][] dp = new int[coins.length][amount + 1];
|
||||
// 只有一种硬币的情况
|
||||
for (int i = 0; i <= amount; i += coins[0]) {
|
||||
dp[0][i] = 1;
|
||||
}
|
||||
for (int i = 1; i < coins.length; i++) {
|
||||
for (int j = 0; j <= amount; j++) {
|
||||
// 第i种硬币使用0~k次,求和
|
||||
for (int k = 0; k * coins[i] <= j; k++) {
|
||||
dp[i][j] += dp[i - 1][j - k * coins[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[coins.length - 1][amount];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue