题目:
题解:
int change(int amount, int* coins, int coinsSize) {
int dp[amount + 1];
bool valid[amount + 1];
memset(dp, 0, sizeof(dp));
memset(valid, false, sizeof(valid));
dp[0] = 1;
valid[0] = true;
for (int i = 0; i < coinsSize; i++) {
for (int j = coins[i]; j <= amount; j++) {
valid[j] |= valid[j - coins[i]];
}
}
if (!valid[amount])
return 0;
for (int i = 0; i < coinsSize; i++) {
for (int j = coins[i]; j <= amount; j++) {
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}