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

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

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

说明:

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

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

思路:

代码

public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
dfs(candidates, result, new ArrayList<>(), target, 0);
return result;
} void dfs(int[] candidates, List<List<Integer>> result, List<Integer> list, int target, int start) {
if (target == 0) {
result.add(new ArrayList<>(list));
} else if (target > 0) {
for (int i = start; i < candidates.length && candidates[i] <= target; ++i) {
list.add(candidates[i]);
dfs(candidates, result, list, target - candidates[i], i);
list.remove(list.size() - 1);
}
}
}

说明

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

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

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

candidates[i] <= target

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

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

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

说明:

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

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

代码

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
dfs(candidates, result, new ArrayList<>(), target, 0);
return result;
} void dfs(int[] candidates, List<List<Integer>> result, List<Integer> list, int target, int start) {
if (target == 0) {
result.add(new ArrayList<>(list));
} else if (target > 0) {
for (int i = start; i < candidates.length && candidates[i] <= target; ++i) {
if (i > start && candidates[i] == candidates[i - 1]) {
continue;
}
list.add(candidates[i]);
dfs(candidates, result, list, target - candidates[i], i + 1);
list.remove(list.size() - 1);
}
}
}

组合总和3——指定个数

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

说明:

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

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

代码

public List<List<Integer>> combinationSum3(int k, int n) {
int[] candidate = {1,2,3,4,5,6,7,8,9};
List<List<Integer>> result = new ArrayList<>();
dfs(candidate, result, new ArrayList<>(), n, k, 0);
return result;
}
void dfs(int[] candidate, List<List<Integer>> result, List<Integer> list, int target, int k, int start) {
if (target == 0) {
if (list.size() == k) {
result.add(new ArrayList<>(list));
}
} else if (target > 0) {
for (int i = start; i < candidate.length && candidate[i] <= target; ++i) {
if (list.size() >= k) {
continue;
}
list.add(candidate[i]);
dfs(candidate, result, list, target - candidate[i], k, i+1);
list.remove(list.size() - 1);
}
}
}

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. 通过Solrj实现对索引库中数据的局部更新操作

    for (UpdateIndexDTO updateIndexDTO : data) { // 局部更新 SolrInputDocument doc = new SolrInputDocument() ...

  2. MySQL数据查询之多表查询

    多表查询 多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment PRIMARY KEY, dna ...

  3. 阿里云ECS装LAMP环境

    学生计划9.9买个一个ECS,要做PHP开发,所以搭建一个Lamp的环境 1.使用镜像,附件 sh-1.5.5附于文后. 2.一键安装 2.1 输入命令:chmod –R 777 sh-1.5.5 c ...

  4. kbmmw 做REST 服务签名认证的一种方式

    一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...

  5. Awesome Python 中文版

    Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文 ...

  6. 参考文献bib管理

    比如在IEEE模板中,在当前目录添加 bib 文件reference.bib 在 \end{document} 之前加入 \bibliographystyle{IEEEtran} \bibliogra ...

  7. Struts2学习第三天——保存登陆信息及数据校验

    在JSP中服务器端保存数据的作用域通常有request.session.application,它们对应的Servlet API分别是HttpServletRquerst.HttpSession.Se ...

  8. Git学习篇之git remote add origin错误

    提示出错信息:fatal: remote origin already exists. 解决办法如下: 1.先输入$ git remote rm origin 2.再输入$ git remote ad ...

  9. WPF 依赖属性&附加属性

    依赖属性 暂无 附加属性 1.在没有控件源码的前提下增加控件的属性 2.多个控件需要用到同一种属性 使用附加属性可以减少代码量,不必为每一个控件都增加依赖属性 3.属性不确定是否需要使用 在某些上下文 ...

  10. OSLab多线程

      日期:2019/3/26 内容:多线程. 一.基本知识 线程的定义 线程(thread)是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单 ...