DFS-20190206
找出所有方案
排列和组合问题
排列:
https://www.lintcode.com/problem/combination-sum/description
public class Solution {
/**
* @param candidates: A list of integers
* @param target: An integer
* @return: A list of lists of integers
*/
public List<List<Integer>> combinationSum(int[] candidates, int target) {
// write your code here
List<List<Integer>> results = new ArrayList<>();
if(candidates == null){
return results;
}
Arrays.sort(candidates);
List<Integer> combination = new ArrayList<>();
helper(candidates,0,results,target,combination);
return results;
}
//1.定义:找到所有combination开头的组合,后面的和是target的组合
private void helper(int[] candidates,
int startIndex, List<List<Integer>> results,
int target,List<Integer> combination){
//3.出口
if(target==0){
results.add(new ArrayList<Integer>(combination));
return;
}
//2.拆解
for(int i = startIndex;i<candidates.length;i++){
if(target<candidates[i]){
break;
}
if(i-1>=0 && candidates[i]==candidates[i-1]){
continue;
}
combination.add(candidates[i]);
helper(candidates,i,results,target-candidates[i],combination);
combination.remove(combination.size()-1);
}
}
}
https://www.lintcode.com/problem/combination-sum-ii/description
public class Solution {
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
public List<List<Integer>> combinationSum2(int[] num, int target) {
// write your code here
List<List<Integer>> results = new ArrayList<>();
if(num == null){
return results;
}
Arrays.sort(num);
List<Integer> combination = new ArrayList<>();
helper(num,0,results,target,combination);
return results;
}
private void helper(int[]num,int startIndex,List<List<Integer>> results,
int target,List<Integer> combination){
if(target == 0){
results.add(new ArrayList<Integer>(combination));
return;
}
for(int i = startIndex;i<num.length;i++){
if(target<num[i]){
break;
}
if(i-1>=0 && i!=startIndex && num[i]==num[i-1]){
continue;
}
combination.add(num[i]);
helper(num,i+1,results,target-num[i],combination);
combination.remove(combination.size()-1);
}
}
}
切割问题
N个字母的字符串对应N-1个数字的组合
N个字符的字符串 子串:O(N^2)个
排列:
https://www.lintcode.com/problem/permutations/description
public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
// write your code here
List<List<Integer>> rst = new ArrayList<>();
if(nums==null){
return rst;
}
if(nums.length ==0){
rst.add(new ArrayList<>());
return rst;
}
List<Integer> list = new ArrayList<>();
helper(rst,list,nums);
return rst;
}
private void helper(List<List<Integer>> rst, List<Integer> list, int[] nums){
if(list.size()==nums.length){
rst.add(new ArrayList<Integer>(list));
return;
}
for(int i=0;i<nums.length;i++){
if(list.contains(nums[i])){
continue;
}
list.add(nums[i]);
helper(rst,list,nums);
list.remove(list.size()-1);
}
}
}
https://www.lintcode.com/problem/permutations-ii/description
public class Solution {
/*
* @param : A list of integers
* @return: A list of unique permutations
*/
public List<List<Integer>> permuteUnique(int[] nums) {
// write your code here
List<List<Integer>> rst = new ArrayList<>();
if(nums==null){
return rst;
}
if(nums.length ==0){
rst.add(new ArrayList<>());
return rst;
}
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();
int[] visited = new int[nums.length];
helper(rst,list,nums,visited);
return rst;
}
private void helper(List<List<Integer>> rst, List<Integer> list, int[] nums,int[] visited){
if(list.size()==nums.length){
rst.add(new ArrayList<Integer>(list));
return;
}
for(int i=0;i<nums.length;i++){
if(visited[i]==1){
continue;
}
if(i-1>=0 && nums[i]==nums[i-1] && visited[i-1]==0){
continue;
}
list.add(nums[i]);
visited[i]=1;
helper(rst,list,nums,visited);
list.remove(list.size()-1);
visited[i]=0;
}
}
};
DFS 时间复杂度:答案个数*构造每个答案的时间
DP:状态个数*计算每个状态的时间
DFS-20190206的更多相关文章
- 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 ...
随机推荐
- Spring下集成ActiveMQ推送
本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现. 首先是spring的applicationContext的配置,如下 <?xml version=&q ...
- 改善C#公共程序类库质量的10种方法(转)
出处:http://www.cnblogs.com/JamesLi2015/p/3140897.html 最近重构一套代码,运用以下几种方法,供参考. 1 公共方法尽可能的使用缓存 public s ...
- Html::a 生成 method=post
<?= Html::a(Yii::t('app', 'delete'), ['delete', 'id' => $model->id], [ 'class' => 'btn b ...
- 解决jeesite开发java.lang.String cannot be cast to com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm$Principal问题
解决jeesite问题java.lang.String cannot be cast to SystemAuthorizingRealm问题 这些天在jeesite项目上进行二次开发,遇到许多莫名其妙 ...
- 一、Numpy基础--数组
(一)Numpy数组对象 Numpy中的nadrray是一个多维数组对象,该对象由两部分组成: 实际的数据 描述这些数据的元数据 大部分的数组操作仅仅修改元数据部分,而不改变底层的实际数据. 数组的数 ...
- Activity Fragment转场动画
Activity转场动画 先介绍个动画的好例子:https://github.com/lgvalle/Material-Animations Activity的转场动画是通过overridePendi ...
- 用JAVA实现无等待数据库连接池
我们都知道数据库连接是一种有限和非常昂贵的应用资源,怎样对这些资源进行高效的管理,能有效的改善整个系统的性能和健壮性.数据库连接池正是针对这个问题而提出来的. 数据库连接负责分配.释放和管理数据库连接 ...
- memcached整理の实践
对于memcached使用内存来存取数据,一般情况下,速度比直接从数据库或者文件系统存取要快,memcached最常用的场景是利用其“存取快”来保护数据库,防止高频率存取数据库. 缓存数据库查询结果 ...
- Go语言最佳实践—— 字符串
1.串联字符串 Go语言虽然支持+=操作符来追加字符串,但更好的方式是使用bytes.Buffer,这种方式在节省内存和效率方面有更好的表现. 如: var buffer bytes.Buffer b ...
- python学习之路 七 :生成器、迭代器
本节重点: 掌握列表生成式.生成器.迭代器 一.生成式 现在有个需求,把[1,2,3,4,5,6,7,8,9,10]中的每个值加1. # 二逼青年版 a = [0,1,2,3,4,5,6,7,8,9 ...