There is no record of this problem, write it:
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 is chosen from C unlimited number of times.
Note:
- All numbers (including target) would be positive integers.
- Elements in a combination (a1, a 2, ..., aK) must is in non-descending order. (ie, a1≤ a2≤ ... ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set and 2,3,6,7
target 7
,
A Solution set is:
[7]
[2, 2, 3]
1 //17:102 classSolution {3 Private:4vector<vector<int> >Res;5 Public:6vector<vector<int> > Combinationsum (vector<int> &candidates,inttarget) {7 res.clear ();8 sort (Candidates.begin (), Candidates.end ());9vector<int>path;TenDFS (candidates, candidates.size ()-1, target, path); One returnRes; A } - - voidDFS (vector<int>& candidates,intPosintTarget, vector<int>&path) { the if(target = =0) { - res.push_back (path); - Reverse (Res.back (). Begin (), Res.back (). end ()); - return; + } - if(Target <0|| POS <0) { + return; A } at - intCURV =Candidates[pos]; - - //don ' t use the current value -DFS (candidates, POS-1, target, path); - intCNT =0; in while(Target >0) { - Path.push_back (CURV); toTarget-=curv; +DFS (candidates, POS-1, target, path); -cnt++; the } * $Path.resize (Path.size ()-CNT);Panax Notoginseng } -};
Leetcode Combination Sum