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 ...
随机推荐
- C#读取EXCEL到内存
public class ExcelUtils { private static string strcon = "Server=48.12.1.28;initial catalog=NBS ...
- 如何将.pem转换为.crt和.key
如何将.pem转换为.crt和.key? 摘自: https://vimsky.com/article/3608.html 貔貅 技术问答 2017-10-08 23:11 certifica ...
- STL中 map 和 multimap
1. 所在头文件<map>. 命名空间std, 声明如下: namespace std{ template <class Key,class T, class Compare = l ...
- 使用VMware克隆Linux系统
最近在学习使用solr云技术,因为是用来学习操作,因此需要在一台虚拟机上,安装多台LinuxOS. 但是又想偷懒,不想每安装一个LinuxOS,就重新配置Linux环境,所以使用克隆,只需安装好一个模 ...
- Spark 0.9.1和Shark 0.9.1分布式安装指南
目录 目录 1 1. 约定 1 2. 安装Scala 1 2.1. 下载 2 2.2. 安装 2 2.3. 设置环境变量 2 3. 安装Spark 2 3.1. 部署 2 3.2. 下载 3 3.3. ...
- Gym - 101498G(Super Subarray )
In this problem, subarray is defined as non-empty sequence of consecutive elements. We define a suba ...
- 洛谷P1501 [国家集训队]Tree II(打标记lct)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- Dubbo RPC源码解读
https://yq.aliyun.com/articles/272405#27 本文代码摘录的时候,将一些与本流程无关的内容去掉了,如有需要请看源码. 一.闲言碎语 使用rpc框架已经多年了,虽然之 ...
- C++ 的写好库编译好,DELPHI或者Java做界面,iOS 和 Android 就都搞定。
当然也可以使用BCB和相关的开发库来开发App,只是别人没法帮助你. 摘自<想到做到-Android开发关键技术与精彩案例>.(詹建飞) p40
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...