DFS-20190206
找出所有方案
排列和组合问题
排列:
https://www.lintcode.com/problem/combination-sum/description
1 public class Solution { 2 /** 3 * @param candidates: A list of integers 4 * @param target: An integer 5 * @return: A list of lists of integers 6 */ 7 public List<List<Integer>> combinationSum(int[] candidates, int target) { 8 // write your code here 9 List<List<Integer>> results = new ArrayList<>(); 10 if(candidates == null){ 11 return results; 12 } 13 Arrays.sort(candidates); 14 15 List<Integer> combination = new ArrayList<>(); 16 helper(candidates,0,results,target,combination); 17 18 return results; 19 } 20 21 //1.定义:找到所有combination开头的组合,后面的和是target的组合 22 private void helper(int[] candidates, 23 int startIndex, List<List<Integer>> results, 24 int target,List<Integer> combination){ 25 //3.出口 26 if(target==0){ 27 results.add(new ArrayList<Integer>(combination)); 28 return; 29 } 30 //2.拆解 31 for(int i = startIndex;i<candidates.length;i++){ 32 if(target<candidates[i]){ 33 break; 34 } 35 36 if(i-1>=0 && candidates[i]==candidates[i-1]){ 37 continue; 38 } 39 40 combination.add(candidates[i]); 41 helper(candidates,i,results,target-candidates[i],combination); 42 combination.remove(combination.size()-1); 43 } 44 } 45 }
View Code
https://www.lintcode.com/problem/combination-sum-ii/description
1 public class Solution { 2 /** 3 * @param num: Given the candidate numbers 4 * @param target: Given the target number 5 * @return: All the combinations that sum to target 6 */ 7 public List<List<Integer>> combinationSum2(int[] num, int target) { 8 // write your code here 9 List<List<Integer>> results = new ArrayList<>(); 10 if(num == null){ 11 return results; 12 } 13 14 Arrays.sort(num); 15 16 List<Integer> combination = new ArrayList<>(); 17 helper(num,0,results,target,combination); 18 19 return results; 20 } 21 22 private void helper(int[]num,int startIndex,List<List<Integer>> results, 23 int target,List<Integer> combination){ 24 if(target == 0){ 25 results.add(new ArrayList<Integer>(combination)); 26 return; 27 } 28 29 for(int i = startIndex;i<num.length;i++){ 30 if(target<num[i]){ 31 break; 32 } 33 34 if(i-1>=0 && i!=startIndex && num[i]==num[i-1]){ 35 continue; 36 } 37 38 combination.add(num[i]); 39 helper(num,i+1,results,target-num[i],combination); 40 combination.remove(combination.size()-1); 41 } 42 } 43 44 }
View Code
切割问题
N个字母的字符串对应N-1个数字的组合
N个字符的字符串 子串:O(N^2)个
排列:
https://www.lintcode.com/problem/permutations/description
1 public class Solution { 2 /* 3 * @param nums: A list of integers. 4 * @return: A list of permutations. 5 */ 6 public List<List<Integer>> permute(int[] nums) { 7 // write your code here 8 List<List<Integer>> rst = new ArrayList<>(); 9 if(nums==null){ 10 return rst; 11 } 12 13 if(nums.length ==0){ 14 rst.add(new ArrayList<>()); 15 return rst; 16 } 17 18 List<Integer> list = new ArrayList<>(); 19 helper(rst,list,nums); 20 return rst; 21 } 22 23 private void helper(List<List<Integer>> rst, List<Integer> list, int[] nums){ 24 if(list.size()==nums.length){ 25 rst.add(new ArrayList<Integer>(list)); 26 return; 27 } 28 29 for(int i=0;i<nums.length;i++){ 30 if(list.contains(nums[i])){ 31 continue; 32 } 33 34 list.add(nums[i]); 35 helper(rst,list,nums); 36 list.remove(list.size()-1); 37 } 38 } 39 }
View Code
https://www.lintcode.com/problem/permutations-ii/description
1 public class Solution { 2 /* 3 * @param : A list of integers 4 * @return: A list of unique permutations 5 */ 6 public List<List<Integer>> permuteUnique(int[] nums) { 7 // write your code here 8 List<List<Integer>> rst = new ArrayList<>(); 9 if(nums==null){ 10 return rst; 11 } 12 13 if(nums.length ==0){ 14 rst.add(new ArrayList<>()); 15 return rst; 16 } 17 18 Arrays.sort(nums); 19 List<Integer> list = new ArrayList<>(); 20 int[] visited = new int[nums.length]; 21 helper(rst,list,nums,visited); 22 return rst; 23 } 24 25 private void helper(List<List<Integer>> rst, List<Integer> list, int[] nums,int[] visited){ 26 if(list.size()==nums.length){ 27 rst.add(new ArrayList<Integer>(list)); 28 return; 29 } 30 31 for(int i=0;i<nums.length;i++){ 32 if(visited[i]==1){ 33 continue; 34 } 35 if(i-1>=0 && nums[i]==nums[i-1] && visited[i-1]==0){ 36 continue; 37 } 38 39 list.add(nums[i]); 40 visited[i]=1; 41 42 helper(rst,list,nums,visited); 43 44 list.remove(list.size()-1); 45 visited[i]=0; 46 } 47 } 48 };
View Code
DFS 时间复杂度:答案个数*构造每个答案的时间
DP:状态个数*计算每个状态的时间
转载于:https://www.cnblogs.com/lizzyluvcoding/p/10354366.html
DFS-20190206相关推荐
- [JS][dfs]题解 | #迷宫问题#
题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...
- [C] [编程题]连通块(DFS解决)
时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 256M,其他语言512M 来源:牛客网 金山办公2020校招服务端开发工程师笔试题(一) 题目描述 给一个01矩阵,1代表是陆地,0代表 ...
- 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组
[BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...
- usaco snail trails(dfs)
dfs啊,我还写了好长时间,一天不如一天. /* ID:jinbo wu TASK: snail LANG:C++ */ #include<bits/stdc++.h> using nam ...
- usaco shuttle puzzle(dfs剪枝)
这题一看我也以为找规律,然后无法下手之后又想到bfs最后看题解是用dfs大神dfs用的出神入化. 不过这题好像可以找规律. /* ID:jinbo wu TASK: shuttle LANG:C++ ...
- usaco street race(dfs)
一开始我觉得很麻烦但第一题好做由于数据较小直接每个点删后dfs就可以了,第二问我不知道如何判断有没有公共的道路,最后看别人的也挺简单的就是看分别从两条公路的起点开始dfs如果他们能到达同一点就代表有公 ...
- Forest Program dfs+tanjar仙人掌
题目链接 CCPC2019 F题. 题意:给一颗仙人掌树,让你求每一个小环的边的个数,用快速幂即可求解. 思路:第一反应是tanjar乱搞,把每个环上的点取出来,类似于缩点的方法.但是忽然感觉dfs能 ...
- HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化
题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...
- A and B and Lecture Rooms CodeForces - 519E LCA+dfs序
看到这个题的第一个思路就是就是统计以每一个点为根的所有节点个数,然后具体就分情况讨论一下即可. 因为刚刚学习了dfs序,这个题就用了dfs序来通过进出时间戳来相减表示其为根的子节点个数. 分情况 我们 ...
- POJ - 2763 Housewife Wind LCA+dfs序+线段树
q次询问求两个点之间的距离,并且可以随时修改某条边的长度,最短距离可以用lca来求,但是树上维护每一个点到root的距离必须要用dfs序来记录时间戳,在dfs的时候顺便记录每一条边(u,v)对应的v节 ...
最新文章
- ceph存储引擎bluestore解析
- 月薪20k-50k| 西人马3D机器视觉算法、语音识别、DSP软件工程师招聘
- ubuntu 12.04/11.10 PPA 安装 Nvidia 295.59
- Numpy-矩阵的合并
- HRESULT:0x80070057 (E_INVALIDARG)的异常的解决方案
- 猫眼(门镜)中的光学
- php - 获取数组头尾元素
- 三位深度学习之父共获2019年图灵奖,学术人生令人赞叹!!!
- SQl触发器 声明变量。
- 【PS】制作水彩画效果
- 长沙python培训_长的解释|长的意思|汉典“长”字的基本解释
- 已解决(Python爬虫requests库报错 请求异常SSL错误,证书认证失败问题)requests.exceptions.SSLError: HTTPSConnectionPool
- 一般3d模型代做多少钱_代做su模型一般怎么收费
- 掌握PS制作,实时预览你的精彩作品
- HarmonyOS 开源开放毫无保留 该英文名有何由来?
- 最新PTCMS小说站系统源码+带采集/会员机制/收费机制
- 测试通达信终端数据接口
- 《安全周报》2010年12月第5期
- Photoshop 2020 入门到精通视频教程共57节课程
- android 拦截点击事件,Android双击事件拦截方法