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 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
2,3,6,7and target
7,
A Solution set is:
[7]
[2, 2, 3]
1 classSolution2 {3 Public:4 voidGeneratecombination (vector<vector<int> > &ret,Constvector<int> &candidates, vector<int> &temp,intTemp_target,intindex)5 {6 for(intI=index; I<candidates.size (); i++)7 {8 if(Temp_target = =0)9 Ret.push_back (temp);Ten One if(Candidates[i] <=temp_target) A { - Temp.push_back (Candidates[i]); -Generatecombination (ret, candidates, temp, temp_target-Candidates[i], i); the Temp.pop_back (); - } - Else - return; + } - } +vector<vector<int> > Combinationsum (vector<int> &candidates,inttarget) A { atvector<vector<int> >ret; -vector<int>temp; - sort (Candidates.begin (), Candidates.end ()); -Generatecombination (ret, candidates, temp, target,0); - - returnret; in } -};
[Leetcode] Combination Sum