LeetCode Top 100 Liked Questions 39. Combination Sum(Java版; Medium)

welcome to my blog

LeetCode Top 100 Liked Questions 39. Combination Sum(Java版; Medium)

题目描述
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]
Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
 [2,2,2,2],
 [2,3,3],
 [3,5]
]
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        List<Integer> al = new ArrayList<>();
        //方便剪枝
        Arrays.sort(candidates);
        core(list, al, candidates, 0, target);
        return list;
    }

    //这里的index不是指nums[index]这个元素, 而是代表[index,n-1]的所有元素. 将[index, n-1]的每个元素选一遍
    private void core(List<List<Integer>> list, List<Integer> al, int[] nums, int index, int target){
        if(target<0){
            return;
        }
        if(target==0){
            list.add(new ArrayList<>(al));
            return;
        }
        for(int i=index; i<nums.length; i++){
            //剪枝
            if(nums[i]>target){
                break;
            }
            al.add(nums[i]);
            core(list, al, nums, i, target-nums[i]);
            al.remove(al.size()-1);
        }
    }
}


第一次做, 要知道什么时候用回溯法: 需要尝试各种可能, 每种可能出现的次数也不确定, 此时就要考虑使用回溯法; 注意回溯法中的更改现场和恢复现场两步操作; 无限调用递归函数会使栈溢出, 检查逻辑时看看会不会出现无限调用的情况
/*
需要尝试各种可能的选择, 每一种选择可能需要重复多次, 这种情况下就要考虑使用回溯法
*/
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if(candidates==null || candidates.length==0)
            return res;
        //
        Core(candidates, 0, res, new ArrayList<Integer>(), target);
        return res;
    }
    public void Core(int[] candidates, int index, List<List<Integer>> res, List<Integer> al, int target){
        //base case
        if(target < 0)
            return;
        if(target == 0){
            res.add(new ArrayList<>(al));
        }
        //可以选自己, 可以选择下一个
        //别从i=0开始, 会出现重复, 如{2,2,3,3}和{3,3,2,2}
        for(int i=index; i<candidates.length; i++){
            //更改现场
            al.add(candidates[i]);
            //无限调用递归函数会使栈溢出, 检查逻辑时看看会不会出现无限调用的情况
            Core(candidates, i, res, al, target-candidates[i]);
            //恢复现场
            al.remove(al.size()-1);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值