Merge pull request #1983 from hoxiansen/hoxiansen-pr

Update 0518.零钱兑换II.md 补充二维dp数组的Java代码
This commit is contained in:
程序员Carl
2023-04-09 09:56:17 +08:00
committed by GitHub

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