Update 0518.零钱兑换II.md 补充二维dp数组的Java代码

This commit is contained in:
hoxiansen
2023-03-28 09:40:19 +00:00
parent f51773db0d
commit 334f4a27de

View File

@ -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 Python