Golang | Leetcode Golang题解之第518题零钱兑换II
题目:
题解:
func change(amount int, coins []int) int {
dp := make([]int, amount+1)
valid := make([]bool, amount+1)
dp[0] = 1
valid[0] = true
for _, coin := range coins {
for i := coin; i <= amount; i++ {
valid[i] = valid[i] || valid[i-coin]
}
}
if !valid[amount]{
return 0
}
for _, coin := range coins {
for i := coin; i <= amount; i++ {
dp[i] += dp[i-coin]
}
}
return dp[amount]
}