您的位置:首页 > 其它

(回溯法)解决一系列组合问题

2017-02-03 14:41 309 查看
题目一:

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],
]


给你两个整数 n和k,从1-n中选择k个数字的组合。比如n=4,那么从1,2,3,4中选取两个数字的组合

  解题思路:http://www.cnblogs.com/Kobe10/p/6361336.html

题目二:

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 set2,3,6,7and target7,
A solution set is:
[7]
[2, 2, 3]


给你一个正数数组candidate[],一个目标值target,寻找里面所有的不重复组合,让其和等于target,给你[2,3,6,7] 2+2+3=7 ,7=7,所以可能组合为[2,2,3],[7])

  解题思路:http://www.cnblogs.com/Kobe10/p/6361336.html

题目三:

给定候选数字(C)和目标数字(T)的集合,找到C中的所有唯一组合,其中候选数字总计为T.

C中的每个数字只能在组合中使用一次。

注意:

所有数字(包括目标)将是正整数。
组合(a1,a2,...,ak)中的元素必须以非降序排列。 (即,a 1≤a2≤...≤ak)。
解集必须不包含重复组合。

例如,给定候选集10,1,2,7,6,1,5和target8,
解决方案集是:
[1,7]
[1,2,5]
[2,6]
[1,1,6]


思路:这个题目和第二题是一个类型的题目,但是他要求的是出现的组合中不能出现重复的数字。所以就要设置一个剪枝函数来限制出现重复的元素,对每一个进入的元素进行判断,看当前元素是否等于上一个元素(因为是递增的关系,所以只要比较上一个元素即可),如果等于就跳过这个元素,继续进行运算。

代码

class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int> > res;
sort(num.begin(),num.end());
vector<int> temp;
backtrack(res,num,temp,target,0);
return res;
}
void backtrack(vector<vector<int> > &res,vector<int> &candidates, vector<int> &temp,int target,int start){
if (target < 0)
return;
else if (target == 0){
res.push_back(temp);
return ;
}

for(int i=start;i<candidates.size();i++){
if(i>start && candidates[i]==candidates[i-1])continue;//i>start表示第一次不需要进行判断,之后再进行判断

temp.push_back(candidates[i]);
backtrack(res,candidates,temp,target-candidates[i],i+1);
temp.pop_back();
}

}
};


题目四:

给定一组不同的整数S,返回所有可能的子集。

注意:

子集中的元素必须以非降序排列。
解集必须不包含重复的子集。

例如,
如果S = [1,2,3],解决方案是:

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


思路:这个题目和第一个题目有点类似。这个题目是数组的所有的组合,而第一题是指定个数的组合数。所以这个是输出所有的,在第一题目的基础上循环输出给定数目的组合数。

代码:

class Solution {
public:
//这道题也是回溯法的套路啊,题目类似于Combinations啊,但是这个是所有的组合,而那个是指定个数的组合,所以只要循环的进行
//回溯就可以实现所有的组合
vector<vector<int> > res;
vector<vector<int> > subsets(vector<int> &S) {
vector<int> temp;
sort(S.begin(), S.end());
for (int i=0; i<=S.size(); i++)
backtrack(S, temp, 0, i);
return res;
}
void backtrack(vector<int> &S, vector<int> temp, int start, int k){
if (k < 0)
return ;
else if (k == 0)
res.push_back(temp);
else{
for (int i=start; i<S.size(); i++){
temp.push_back(S[i]);
backtrack(S, temp, i+1, k-1);
temp.pop_back();
}
}
}
};


题目五:

给定一个可能包含重复S的整数集合,返回所有可能的子集。

注意:

子集中的元素必须按照降序排列。
解集必须不包含重复的子集。

例如,
如果S = [1,2,2],解决方案是:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]]


思路:这里加上了重复的元素,所以直接处理看前后两个元素是否相等。如果相等就跳过这个元素。
    

代码:

class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<int> temp;
sort(S.begin(), S.end());
for (int i=0; i<=S.size(); i++)
backtrack(S, temp, 0, i);
return res;
}
void backtrack(vector<int> &S, vector<int> temp, int start, int k){
if (k < 0)
return ;
else if (k == 0)
res.push_back(temp);
else{
for (int i=start; i<S.size(); i++){
if (i>start && S[i] == S[i-1])
continue;
temp.push_back(S[i]);
backtrack(S, temp, i+1, k-1);
temp.pop_back();
}
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: