标签:
This is a classical combination question.
1 class Solution { 2 public: 3 void getComb(vector<vector<int> > &result, const vector<int> &num, vector<int> current, int sum, int target, int start) { 4 if (sum == target) { 5 result.push_back(current); 6 return; 7 } 8 if (sum > target) { 9 return; 10 } 11 for (int i = start; i < num.size(); i++) { 12 current.push_back(num[i]); 13 getComb(result, num, current, sum + num[i], target, i); 14 current.pop_back(); 15 } 16 } 17 vector<vector<int> > combinationSum(vector<int> &candidates, int target) { 18 vector<vector<int> > result; 19 sort(candidates.begin(), candidates.end()); 20 getComb(result, candidates, vector<int> (), 0, target, 0); 21 return result; 22 } 23 };
LeetCode – Refresh – Combination Sum
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/4349268.html