DFS经典组合问题

下面将遇到的可以用递归求解的问题归纳于此


1. Combination

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]

【思路】递归

1.用一临时数组solution存储元素, 其大小应为k

2 base case , solution长度为k,并弹出最后元素

3 else 继续向solution push元素

注意:需要引用传参

[cpp]  view plain copy
  1. void getCombine(  
  2.                 int n,  
  3.                 int k,  
  4.                 int level,  
  5.                 vector<int> &solution,  
  6.                 vectorint>> &result){  
  7.     if(solution.size() == k){  
  8.         result.push_back(solution);  
  9.         return;  
  10.     }  
  11.     for(int i = level; i<=n; i++){  
  12.         solution.push_back(i);  
  13.         getCombine(n,k,i+1,solution,result);  
  14.         solution.pop_back();  
  15.     }  
  16. }  
  17. vectorint> > combine(int n, int k) {  
  18.     vector<int> solution;  
  19.     vectorint>> result;  
  20.     getCombine(n,k,1,solution,result);  
  21.     return result;  
  22. }  



2. Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7, 
A solution set is: 
[7] 
[2, 2, 3]

【思路】 递归, 类似于上一题

base case: solution中元素之和sum为target

if sum值大于target 直接返回,并弹出最后元素

else 从candidate numbers 中继续选出元素加到solution中,同时用sum记录当前和值

注意:

1.结果中元素应保持non-descending order, 应先排序

2. 结果可以包含重复元素,递归中直接传level值就可以了,和上一题的不同. 


[cpp]  view plain copy
  1. void getCombineSum(  
  2.                    int &sum,  
  3.                    int level,  
  4.                    int target,  
  5.                    vector<int> &solution,  
  6.                    vector<int> &candidates,  
  7.                    vectorint>> &result  
  8.                    ){  
  9.     if(sum > target) return;  
  10.     if(sum == target){  
  11.         result.push_back(solution);  
  12.         return;  
  13.     }  
  14.     for(int i = level; i
  15.         sum += candidates[i];  
  16.         solution.push_back(candidates[i]);  
  17.         getCombineSum(sum,"color:#ff6666;">i,target,solution,candidates,result);  
  18.         solution.pop_back();  
  19.         sum -= candidates[i];  
  20.     }  
  21.   
  22. }  
  23. vectorint> > combinationSum(vector<int> &candidates, int target) {  
  24.     vector<int> solution;  
  25.     vectorint>> result;  
  26.     int sum = 0;  
  27.     sort(candidates.begin(),candidates.end());  
  28.     getCombineSum(sum,0,target,solution,candidates,result);  
  29.     return result;  
  30. }  

3.Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5 and target 8, 
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

【思路】 与上一题类似,不同之处在于不能重复添加candidated中的元素

[cpp]  view plain copy
  1. void getCombineSum2(  
  2.                    int &sum,  
  3.                    int level,  
  4.                    int target,  
  5.                    vector<int> &solution,  
  6.                    vector<int> &num,  
  7.                    vectorint>> &result  
  8.                    ){  
  9.     if(sum > target) return;  
  10.     if(sum == target) {  
  11.         result.push_back(solution);  
  12.         return;  
  13.     }  
  14.     for(int i=level; i
  15.         sum += num[i];  
  16.         solution.push_back(num[i]);  
  17.         getCombineSum2(sum,"color:#ff6666;">i+1,target,solution,num,result);  
  18.         sum -= num[i];  
  19.         solution.pop_back();  
  20.         "color:#ff6666;">while(i  
  21.     }  
  22. }  
  23. vectorint> > combinationSum2(vector<int> &num, int target) {  
  24.     vector<int> solution;  
  25.     vectorint>> result;  
  26.     int sum = 0;  
  27.     sort(num.begin(), num.end());  
  28.     getCombineSum2(sum,0,target,solution,num,result);  
  29.     return result;  
  30. }  

你可能感兴趣的:(Algorithm,Leetcode)