Leetcode 216. Combination Sum III

54 篇文章 0 订阅
11 篇文章 0 订阅
public class Solution {
    private static int rounds; 
    
    public static void backTrack(int k, int n, int start, int[] nums, List<Integer> tmp, List<List<Integer>> res) {
        if (k == 0) return;
        else {
            for (int i=start; i<=nums.length-k; i++) {
                tmp.add(nums[i]);
                backTrack(k-1, n-nums[i], i+1, nums, tmp, res);
                // when target is 0 and tmp.size() equals to k means we have reached the node and the sum equals to target,
                // add the solution the result set
                if (tmp.size() == rounds && n-nums[i] == 0) res.add(new ArrayList<>(tmp));
                // before return to the parent level, remove the node we just added 
                tmp.remove(tmp.size()-1);
            }
        }
    }
    
    public List<List<Integer>> combinationSum3(int k, int n) {
        rounds = k;
        int[] nums = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
        List<List<Integer>> res = new ArrayList<>();
        backTrack(k, n, 0, nums, new ArrayList<>(), res);
        return res;
    }
}

Combination Sum I

/**
 * The algorithm's flow chart would be,
 * Step i.   add candi[i] to the tmp list,
 * Step ii.  take care of the scenario which reslut set may have candi[i] by,
 *           if target drops to zero (reach to node), add all elements in tmp list to the result set
 *           then return and remove the last element in tmp list return to the parent. 
 * Step iii. in the for loop, i++, now consider the scenario which reslut set doesn't have candi[i-1]
 */ 
public class Solution {
    public static void backTrack(int start, int target, int[] candi, List<Integer> tmp, List<List<Integer>> res) {
        if (target < 0) return;
        else if (target == 0) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        else {
            // using a for loop tow control if the result set has candi[i] or not
            // every time i increament by 1 which excludes candi[i-1] from the result set
            for (int i=start; i<candi.length; i++) {
                tmp.add(candi[i]);
                backTrack(i, target-candi[i], candi, tmp, res);
                tmp.remove(tmp.size()-1);
            }
        }
    }
    
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        backTrack(0, target, candidates, new ArrayList<>(), res);
        return res;
    }
}

Combination Sum II

public class Solution {
    public static void backTrack(int start, int target, int[] candi, List<Integer> tmp, List<List<Integer>> res) {
        if (target < 0) return;
        else if (target == 0) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        else {
            for (int i=start; i<candi.length; i++) {
                // we've sorted the array, now just check if two nearby elements are equal to avoid duplicates
                // note that i != start should always be placed in front of the rest statement
                // (*)
                if (i != start && candi[i] == candi[i-1]) continue; 
                tmp.add(candi[i]);
                // (*)
                // difference here we cannot apply candi[i] multiple times, 
                // update start to i+1 after we added candi[i] to the result set
                backTrack(i+1, target-candi[i], candi, tmp, res);
                tmp.remove(tmp.size()-1);
            }
        }
    }
    
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        // because the array may contain duplicates, sort the array first
        Arrays.sort(candidates);
        List<List<Integer>> res = new ArrayList<>();
        backTrack(0, target, candidates, new ArrayList<>(), res);
        return res;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值