组合总和——给定元素不重复

需求:给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

  • 所有数字(包括 target)都是正整数。
  • 解集不能包含重复的组合。

示例:candidates = [2,3,5], target = 8。结果[(2,2,2,2),(3,5)]

思路:

代码

  1. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  2. List<List<Integer>> result = new ArrayList<>();
  3. Arrays.sort(candidates);
  4. dfs(candidates, result, new ArrayList<>(), target, 0);
  5. return result;
  6. }
  7.  
  8. void dfs(int[] candidates, List<List<Integer>> result, List<Integer> list, int target, int start) {
  9. if (target == 0) {
  10. result.add(new ArrayList<>(list));
  11. } else if (target > 0) {
  12. for (int i = start; i < candidates.length && candidates[i] <= target; ++i) {
  13. list.add(candidates[i]);
  14. dfs(candidates, result, list, target - candidates[i], i);
  15. list.remove(list.size() - 1);
  16. }
  17. }
  18. }

说明

1.为了不出现(2,3,5),(3,2,5)这样的重合,每次dfs时起始元素为目前所处的位置:

  1. dfs(candidates, result, list, target - candidates[i], i)

2.为了避免无谓的遍历,通过剪枝结束遍历:

  1. candidates[i] <= target

组合总和——给定元素重复

需求:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

示例:candidates = [1,2,2,2,5], target = 5。结果[(1,2,2),(5)]

代码

  1. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  2. List<List<Integer>> result = new ArrayList<>();
  3. Arrays.sort(candidates);
  4. dfs(candidates, result, new ArrayList<>(), target, 0);
  5. return result;
  6. }
  7.  
  8. void dfs(int[] candidates, List<List<Integer>> result, List<Integer> list, int target, int start) {
  9. if (target == 0) {
  10. result.add(new ArrayList<>(list));
  11. } else if (target > 0) {
  12. for (int i = start; i < candidates.length && candidates[i] <= target; ++i) {
  13. if (i > start && candidates[i] == candidates[i - 1]) {
  14. continue;
  15. }
  16. list.add(candidates[i]);
  17. dfs(candidates, result, list, target - candidates[i], i + 1);
  18. list.remove(list.size() - 1);
  19. }
  20. }
  21. }

组合总和3——指定个数

需求:找出所有相加之和为 的 个数的组合组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。

说明:

  • 所有数字都是正整数。
  • 解集不能包含重复的组合。

示例:输入: k = 3, n = 9,输出: [[1,2,6], [1,3,5], [2,3,4]]

代码

  1. public List<List<Integer>> combinationSum3(int k, int n) {
  2. int[] candidate = {1,2,3,4,5,6,7,8,9};
  3. List<List<Integer>> result = new ArrayList<>();
  4. dfs(candidate, result, new ArrayList<>(), n, k, 0);
  5. return result;
  6. }
  7. void dfs(int[] candidate, List<List<Integer>> result, List<Integer> list, int target, int k, int start) {
  8. if (target == 0) {
  9. if (list.size() == k) {
  10. result.add(new ArrayList<>(list));
  11. }
  12. } else if (target > 0) {
  13. for (int i = start; i < candidate.length && candidate[i] <= target; ++i) {
  14. if (list.size() >= k) {
  15. continue;
  16. }
  17. list.add(candidate[i]);
  18. dfs(candidate, result, list, target - candidate[i], k, i+1);
  19. list.remove(list.size() - 1);
  20. }
  21. }
  22. }

dfs问题总结的更多相关文章

  1. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

  10. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

随机推荐

  1. 20172306 2018-2019 《Java程序设计与数据结构》第一周学习总结

    20172306 2018-2019 <Java程序设计与数据结构(下)>第一周学习总结 教材学习内容总结 第一章 概述 (程序=数据结构+算法 软件=程序+软件工程) 1.1 软件质量 ...

  2. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  3. [字符串][NOIP2012]Vigenère密码

    Vigenère密码 题目描述 16世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère密码的加密解密算法简单易用,且破译难度比较高, ...

  4. Paper | 块分割信息 + 压缩视频质量增强

    目录 1. 亮点 2. 网络 3. Mask 及其融合 4. 结论 论文:Enhancing HEVC Compressed Videos with a Partition-Masked Convol ...

  5. BASH 环境

    本节内容 1.  什么是shell 2.  命令的优先级 3.  元字符 4.  登录shell与非登录shell 一  什么是shell shell一般代表两个层面的意思,一个是命令解释器,如bas ...

  6. issue:ssh自动断开

    使用ssh连接云服务器的时候,几分钟不操作terminal就会卡住,实际上ssh连接已经断开了,感觉很不爽.(可能云服务器供应商在系统中做了设置) 解决办法: step1:vim /etc/ssh/s ...

  7. mysql大全

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  8. Latex 模版生成会议论文 不显示Keywords,而是显示 Index Terms- ,改成Keywords 方法

    一. 不管显示何种内容,TEX 文件都是 \begin{IEEEKeywords} 关键词1.关键词2,..... \end{IEEEKeywords} 其中:模版文件 IEEETran.cls存在下 ...

  9. SSD磁盘测试不达标排查

     最近购买了一块4T的Inter_SSD_D3-4510硬盘安装在了一台DELL PowerEdge R640服务器,经过测试发现磁盘和产品手册上描述的性能相差过大,相当于产品手册性能的1/2,一下是 ...

  10. 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-fi ...