39. 组合总和

155 阅读1分钟

1.简单DFS+回溯

class Solution {
    LinkedList<Integer> path = new LinkedList<>();//记录路径
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dfs(candidates, 0, 0, target);
        return res;
    }
    
    //start是遍历开始的index,cunSum为当前分支的和,target为目标和
    public void dfs(int[] candidates, int start, int curSum, int target) {
        if (curSum > target) {//可能永远不=tar,不及时返回的话会无限递归下去
            return;
        }
        if (curSum == target) {//凑齐了
            res.add(new ArrayList<>(path));//把当前路径存储一份到结果集中
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            path.add(candidates[i]);
            dfs(candidates, i, curSum + candidates[i], target);
            path.removeLast();
        }
    }
}

2.剪枝后

class Solution {
    LinkedList<Integer> path = new LinkedList<>();//记录路径
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dfs(candidates, 0, 0, target);
        return res;
    }
    
    //start是遍历开始的index,cunSum为当前分支的和,target为目标和
    public void dfs(int[] candidates, int start, int curSum, int target) {
        if (curSum > target) {//可能永远不=tar,不及时返回的话会无限递归下去
            return;
        }
        if (curSum == target) {//凑齐了
            res.add(new ArrayList<>(path));//把当前路径存储一份到结果集中
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            if (curSum + candidates[i] > target) {//剪枝,避免走到下一层再判断curSum > target
                continue;
            }
            path.add(candidates[i]);
            dfs(candidates, i, curSum + candidates[i], target);// 从i开始,去重复
            path.removeLast();
        }
    }
}