码迷,mamicode.com
首页 > 其他好文 > 详细

DFS-20190206

时间:2019-02-07 09:31:58      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:splay   dba   amp   数字   start   isp   i+1   spl   hat   

找出所有方案

排列和组合问题

排列:

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:状态个数*计算每个状态的时间

 

DFS-20190206

标签:splay   dba   amp   数字   start   isp   i+1   spl   hat   

原文地址:https://www.cnblogs.com/lizzyluvcoding/p/10354366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!