class Solution { public int coinChange(int[] coins, int amount) { int max = amount + 1; // 最小的币值为1,就算全部用1,硬币个数也不会超过amount + 1 int d[] = new int[amount+1]; Arrays.fill(d,max); d[0] = 0; for(int i = 1; i <= amount; i++){ for(int j = 0; j < coins.length; j++){ if(i >= coins[j] ){ if(d[i] > d[i-coins[j]] + 1) d[i] = d[i-coins[j]] + 1; } } } return d[amount] > amount ? -1 : d[amount]; } }