1. 程式人生 > >518. Coin Change 2

518. Coin Change 2

會有 coin == chan bsp ray nbsp 在外 clas

coin循環要在外面 從小到大 不會有重復

 1 class Solution {
 2     public int change(int amount, int[] coins) {
 3         // if(coins.length == 0) return 1;
 4         int[] dp = new int[amount + 1];
 5         dp[0] = 1;
 6         Arrays.sort(coins);
 7         for(int j = 0; j < coins.length; j++){
 8             for
(int i = 1; i <= amount; i++){ 9 if(coins[j] <= i){ 10 dp[i] += dp[i - coins[j]]; 11 } 12 } 13 } 14 return dp[amount]; 15 16 } 17 }

518. Coin Change 2