Day46.算法训练

518. 零钱兑换 II

class Solution {
    public int change(int amount, int[] coins) {
        int dp[] = new int[amount + 1];
        dp[0] = 1;
        // 遍历物品
        for (int i = 0; i < coins.length; i++) {

            // 遍历背包 从小到大
            for (int j = coins[i]; j <= amount; j++) {
                dp[j] += dp[j - coins[i]];    
            }

        }
        return dp[amount];
    }
}

377. 组合总和 Ⅳ

    class Solution {
        public int combinationSum4(int[] nums, int target) {

            int dp[] = new int[target + 1];
            dp[0] = 1;

            // 遍历背包 从小到大
            for (int j = 1; j <= target; j++) {

                // 遍历物品
                for (int i = 0; i < nums.length; i++) {
                    if (j >= nums[i]) {
                        dp[j] += dp[j - nums[i]];
                    }
                }

            }
            return dp[target];
        }
    }

你可能感兴趣的:(算法)