dfs问题总结
组合总和——给定元素不重复
需求:给定一个无重复元素的数组 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——指定个数
需求:找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 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问题总结的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
随机推荐
- Eclipse Python 开发环境搭建 pydev 插件
已安装: python 3.6 JDK Eclispe 在 Eclipse 中安装 pydev Pydev 的下载网址 http://www.pydev.org/download.html 安装完成后 ...
- redis在游戏服务器中的使用初探(二) 客户端开源库选择
上文提到 搭建完成后 我们选择客户端的开源库进行连接 有以下三种选择 1 acl-redis 原因是支持VC 国产 作者博客 acl 框架库简介 用 acl 库编写高效的 C++ redis ...
- Zookeeper系列2 原生API 以及核心特性watcher
原生API 增删改查询 public class ZkBaseTest { static final String CONNECT_ADDR = "192.168.0.120"; ...
- spring用注解配置,不用XML
//首先装载一个配置类AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyCon ...
- 【算法】map的应用
map使用参考链接http://www.cnblogs.com/KID-XiaoYuan/articles/7297709.html 题目 在ACM比赛中,你每解决一道题,你就可以获得一个气球,不同颜 ...
- Android Studio导入第三方jar包或依赖工程的方法
Android Studio导入第三方jar包或依赖工程的方法 一 导入jar包的方法 1.打开自己的工程,将需要导入的jar包copy到libs文件夹下 2.在导入的jar包处单击菜单 Add ...
- lambda 匿名函数
# 普通python函数 def func(a,b,c): return a+b+c print func(1,2,3) # 返回值为6 # lambda匿名函数 f = lambda a,b,c:a ...
- 【repost】前端学习总结(二十三)——前端框架天下三分:Angular React 和 Vue的比较
目录(?)[+] 前端这几年的技术发展很快,细分下来,主要可以分成四个方面: 1.开发语言技术,主要是ES6&7,coffeescript,typescript等: 2.开发框架,如Ang ...
- go的数据库操作mysql
go get github.com/go-sql-driver/mysql package main; import ( "database/sql" _ "github ...
- WeexSDK之注册Handlers
先看代码: + (void)_registerDefaultHandlers { [self registerHandler:[WXResourceRequestHandlerDefaultImpl ...