39.组合总和

class Solution {
    public List> combinationSum(int[] candidates, int target) {
        List> lists = new ArrayList<>();
        if(candidates.length == 0){
            return lists;
        }
        if(target == candidates[candidates.length-1]){
            List list = new ArrayList<>();
            list.add(target);
            lists.add(list);
            List> next = combinationSum(Arrays.copyOfRange(candidates, 0, candidates.length-1), target);
            if(next.size() != 0){
                lists.addAll(next);
            }
            return lists;
        }
        if(target < candidates[candidates.length - 1]){
            return combinationSum(Arrays.copyOfRange(candidates, 0, candidates.length-1), target);
        }
        List> sum = combinationSum(candidates, target-candidates[candidates.length - 1]);
        for(int i = 0; i < sum.size(); i++){
            sum.get(i).add(candidates[candidates.length - 1]);
        }
        lists.addAll(sum);

        sum = combinationSum(Arrays.copyOfRange(candidates, 0, candidates.length-1), target);

        lists.addAll(sum);

        return  lists;
    }
}

你可能感兴趣的:(39.组合总和)