给出一组候选数C和一个目标数T,找出候选数中起来和等于T的所有组合。
C中的每个数字在一个组合中只能使用一次。
注意:
例如:给定的候选数集是[10,1,2,7,6,1,5],目标数是8
解集是:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
Given a collection of candidate numbers ( C ) and a target number
( T ), find all unique combinations in Cwhere the candidate numbers sums
to T .
Each number in C may only be used once in the combination.
Note:
For example, given candidate set10,1,2,7,6,1,5and target8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
class Solution {
vector< int> d;
vector
set
public:
vector
sort(num.begin(),num.end());
dfs(num,0,target,0);
set
for (it=s.begin();it!=s.end();z.push_back(*it),it++);
return z;
}
void dfs(vector
if (x>t || x<0 ) return ;
if (x==t){s.insert(d);}
else
{
int i;
for (i=step;i<candidates.size();i++){
d.push_back(candidates[i]);
dfs(candidates,x+candidates[i],t,i+1);
d.pop_back();
}
}
}
};
手机扫一扫
移动阅读更方便
你可能感兴趣的文章