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 ...
随机推荐
- HTML第一篇
Hyper Text Markup Language 超文本标记语言:是一种创建网页的标准标记语言. <!DOCTYPE html> <html> <head> ...
- python入门之两种方法修改文件内容
1.占硬盘修改 import os file_name="兼职.txt" new_file_name="%s.new".% file_name old_str= ...
- java安装jdk错误1316 指定的账户已存在
java安装jdk错误1316 指定的账户已存在 处理步骤: 1.卸载jdk,成功后重启 2.删除注册表中文件夹 (1)\HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft文件夹 ...
- 子div撑不开父div
方法一:推荐 设置父div的overflow:hidden; 方法二: 父div结束前增加一个空div style=”clear:both;” .clear { clear:both; } <d ...
- verilog 有符号数运算
1)之前的笔记写过<补码探讨>,可知在FPGA综合成电路的时候最底层都是以补码的形式在运算,正数的补码就是本身,负数的补码要取反+1. (2)那么Verilog中编程的时候对编程人员来说, ...
- UART串口通讯协议
一.UART定义 UART 通用异步收发传输器(Universal Asynchronous Receiver/Transmitter),通常称作UART,是一种通用的串行异步全双工数据收发传输器(总 ...
- array_filter()函数
用回调函数过滤数组中的值 array_filter(array,callbackfunction); 返回过滤后的数组
- Dynamic Programming | Set 1 (Overlapping Subproblems Property)
动态规划是这样一种算法范式:将复杂问题划分为子问题来求解,并且将子问题的结果保存下来以避免重复计算.如果一个问题拥有以下两种性质,则建议使用动态规划来求解. 1 重叠子问题(Overlapping S ...
- [转]data-driven与决策树聚类的两种方法
参考文章: http://blog.csdn.net/quheDiegooo/article/details/60873999 http://blog.csdn.net/quhediegooo/art ...
- React-Native 问题随记2: com.android.builder.testing.api.DeviceException
错误详细: Execution failed for task ':app:installDebug'.> com.android.builder.testing.api.DeviceExcep ...