mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #2419 from EmmIons/EmmIons-patch-1
Update 0518.零钱兑换II.md
This commit is contained in:
@ -224,20 +224,26 @@ class Solution {
|
|||||||
// 二维dp数组版本,方便理解
|
// 二维dp数组版本,方便理解
|
||||||
class Solution {
|
class Solution {
|
||||||
public int change(int amount, int[] coins) {
|
public int change(int amount, int[] coins) {
|
||||||
int[][] dp = new int[coins.length][amount + 1];
|
int[][] dp = new int[coins.length][amount+1];
|
||||||
// 只有一种硬币的情况
|
|
||||||
for (int i = 0; i <= amount; i += coins[0]) {
|
// 初始化边界值
|
||||||
dp[0][i] = 1;
|
for(int i = 0; i < coins.length; i++){
|
||||||
|
// 第一列的初始值为1
|
||||||
|
dp[i][0] = 1;
|
||||||
}
|
}
|
||||||
for (int i = 1; i < coins.length; i++) {
|
for(int j = coins[0]; j <= amount; j++){
|
||||||
for (int j = 0; j <= amount; j++) {
|
// 初始化第一行
|
||||||
// 第i种硬币使用0~k次,求和
|
dp[0][j] += dp[0][j-coins[0]];
|
||||||
for (int k = 0; k * coins[i] <= j; k++) {
|
}
|
||||||
dp[i][j] += dp[i - 1][j - k * coins[i]];
|
|
||||||
}
|
for(int i = 1; i < coins.length; i++){
|
||||||
|
for(int j = 1; j <= amount; j++){
|
||||||
|
if(j < coins[i]) dp[i][j] = dp[i-1][j];
|
||||||
|
else dp[i][j] = dp[i][j-coins[i]] + dp[i-1][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dp[coins.length - 1][amount];
|
|
||||||
|
return dp[coins.length-1][amount];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user