1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution { public : vector<vector< int >> combinationSum(vector< int > &candidates, int target) { sort(candidates.begin(), candidates.end()); vector<vector< int >> res; vector< int > combination; combinationSum(candidates, target, res, combination, 0); return res; } private : void combinationSum(vector< int > &candidates, int target, vector<vector< int >> &res, vector< int > &combination, int begin) { if (!target) { res.push_back(combination); return ; } for ( int i = begin; i != candidates.size() && target >= candidates[i]; ++i) { combination.push_back(candidates[i]); combinationSum(candidates, target - candidates[i], res, combination, i); combination.pop_back(); } } }; |