Combination Sum I:
算法题目: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]
题意:给定一个正数集合和一个目标数,求出所有的子集合,使子集的和为target,要求子集合递增排列,且不包含重复的子集合,子集合中的数字可以重复
思路:先排序,递归求所有数字的集合,并判断数是否等于target
void CombinationSumCore(const vector<int>& candidates,vector<vector<int>>& res,vector<int>& com,int target,int pos)
{
if(target==0)
{
res.push_back(com);
return;
}
for(int i=pos;i<candidates.size();i