leetCode40组合总和(回溯)

题目

复制代码
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次

示例 :

复制代码
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

回溯一般模板

1、会使用深度优先遍历的递归

2、数据是否排序

(1)如果不允许数据重复,要先对数组进行排序,递归的时候,起始节点=当前的index+1,而且

同层相同元素不重复执行

(2)数据允许重复,不用对数组排序,递归的时候,起始节点=当前的index

3、完全递归,把数据恢复到上一个状态,也就是回溯

模板总结

参数说明:

|------------|---------------------------|
| start | 开始的数组下标 |
| candidates | 数组 |
| sum | 达到的条件(这里以数据和为例),具体的按题目要求换 |
| resultList | 所有结果 |
| path | 中间过程,符合要求的一组数据 |

允许数据重复一般模板

复制代码
public static void dfsNum(int start, int[] candidates, int target, int sum, List<List<Integer>> resultList, List<Integer> path) {
    if (结束递归条件) {
        return;
    }
    if (符合要求数据条件) {
        resultList.add(new ArrayList<>(path));
        return;
    }
    for (int i = start; i < candidates.length; i++) {
        if (剪枝条件) {
            break;
        }
        sum += candidates[i];
        path.add(candidates[i]);
        dfsNum(i, candidates, target, sum, resultList, path);
         //回溯,过程中使用的数据,恢复原值
        path.remove(path.size() - 1);
        sum -= candidates[i];
    }
}

不允许数据重复一般模板

复制代码
public static void dfsNum(int start, int[] candidates, int target, int sum, List<List<Integer>> resultList,List<Integer> path) {
 if (结束递归条件) { return; } 
 if (符合要求数据条件) {
    resultList.add(new ArrayList<>(path)); 
     return; 
}
    for (int i = start; i < candidates.length; i++) {
        //最外层重复数据不重复执行
        if (i > start && candidates[i] == candidates[i - 1]) {
            continue;
        }
        if (剪枝条件) {
            break;
        }
        sum += candidates[i];
        path.add(candidates[i]);
        dfsNum(i + 1, candidates, target, sum, resultList, path);
 //回溯,过程中使用的数据,恢复原值
        path.remove(path.size() - 1);
        sum -= candidates[i];
    }
}

实现代码

复制代码
public class ArrayZhSum40 {

    public static void main(String[] args) {
        int[] candidates = {2, 5, 2, 1, 2};
        List<List<Integer>> resultList = combinationSum2(candidates, 5);
        System.out.println(JSON.toJSONString(resultList));
        candidates = new int[]{10, 1, 2, 7, 6, 1, 5};
        resultList = combinationSum2(candidates, 8);
        System.out.println(JSON.toJSONString("---" + resultList));
    }


    public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
        if (candidates == null || candidates.length == 0) {
            return null;
        }
        Arrays.sort(candidates);
        List<List<Integer>> resultList = new ArrayList<>();
        dfsNum(0, candidates, target, 0, resultList,
                new ArrayList<Integer>());
        return resultList;
    }

    public static void dfsNum(int start, int[] candidates, int target, int sum, List<List<Integer>> resultList,
                              List<Integer> path) {
        if (target < sum) {
            return;
        }
        if (target == sum) {
            resultList.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
           //分层中使用过的元素直接不在用
            if (i > start && candidates[i] == candidates[i - 1]) {
                continue;
            }
            if (candidates[i] > target) {
                break;
            }
            sum += candidates[i];
            path.add(candidates[i]);
            //叶子中,不使用已经使用过的元素
            dfsNum(i + 1, candidates, target, sum, resultList, path);
            path.remove(path.size() - 1);
            sum -= candidates[i];
        }
    }


}
相关推荐
不知几秋9 分钟前
Spring Boot
java·前端·spring boot
BUG收容所所长1 小时前
栈的奇妙世界:从冰棒到算法的华丽转身
前端·javascript·算法
程序员岳焱1 小时前
深度剖析:Spring AI 与 LangChain4j,谁才是 Java 程序员的 AI 开发利器?
java·人工智能·后端
XRZaaa1 小时前
常见排序算法详解与C语言实现
c语言·算法·排序算法
都叫我大帅哥1 小时前
AQS(AbstractQueuedSynchronizer)深度解剖:从“奶茶店排队”到源码级设计哲学
java
@我漫长的孤独流浪1 小时前
数据结构测试模拟题(4)
数据结构·c++·算法
智驱力人工智能1 小时前
智慧零售管理中的客流统计与属性分析
人工智能·算法·边缘计算·零售·智慧零售·聚众识别·人员计数
斯奕sky_small-BAD1 小时前
C++ if语句完全指南:从基础到工程实践
java·开发语言·php
云之渺1 小时前
125java
java
都叫我大帅哥1 小时前
Java ReentrantLock:从“舔狗式等待”到源码级征服指南
java