---
# System prepended metadata

title: 0518. Coin Change 2
tags: [Leetcode, Dynamic Programming, Medium]

---

# 0518. Coin Change 2
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/coin-change-2/
## 思路
不能算是一个完全背包问题
因为不是求总价值的最优
而是单纯求组合数
```dp[i]```表示用```coins```组合成```i```的方法一共有几种
## Code
```java=
class Solution {
    public int change(int amount, int[] coins) {
        int n = coins.length;
        int[] dp = new int[amount+1];
        dp[0] = 1;
        for(int i=0; i<n; i++){
            for(int j=0; j<=amount; j++){
                if(j>=coins[i]){
                    dp[j] += dp[j-coins[i]];
                }
            }
        }
        return dp[amount];
    }
}
```