給出不同面值的硬幣以及總金額. 試寫一函數來計算構成該總額的組合數量. 你可以假設每一種硬幣你都有無限個.
樣例
樣例1
輸入: amount = 10 和 coins = [10]
輸出: 1
樣例2
輸入: amount = 8 和 coins = [2, 3, 8]
輸出: 3
解釋:
有3種方法:
8 = 8
8 = 3 + 3 + 2
8 = 2 + 2 + 2 + 2
注意事項
你可以做出以下假設:
0 <= amount <= 5000
1 <= coin <= 5000
硬幣種類不超過 500
結果保證符合 32 位符號整數
class Solution {
public:
/**
* @param amount: a total amount of money amount
* @param coins: the denomination of each coin
* @return: the number of combinations that make up the amount
*/
int change(int amount, vector<int> &coins) {
// write your code here
vector<int>dp(amount+1,0);
dp[0]=1;
for (int i = 0; i < coins.size(); i++) {
for(int j = coins[i];j<=amount;j++)
{
dp[j]+=dp[j-coins[i]];
}
}
return dp[amount];
}
};