1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| class Solution { public: vector<vector<int>> combinationSum(vector<int> &candidates, int target) { vector<vector<int>> result; vector<int> book; combinate(result, book, candidates, candidates.size() - 1, target); return result; }
private: void combinate(vector<vector<int>> &result, vector<int> &book, vector<int> &candidates, int index, int target) { if (target == 0) { result.push_back(book); return; } if (target < 0) { return; }
if (index >= 0) { book.push_back(candidates[index]); combinate(result, book, candidates, index, target - candidates[index]); book.pop_back(); combinate(result, book, candidates, index - 1, target); } } vector<vector<int>> getCartesianProduct(vector<vector<int>> &a, vector<vector<int>> &b) { vector<vector<int>> result; for (int i = 0; i < a.size(); i++) { for (int k = 0; k < b.size(); k++) { vector<int> temp; temp.insert(temp.end(), a[i].begin(), a[i].end()); temp.insert(temp.end(), b[k].begin(), b[k].end()); result.push_back(temp); } } return result; } };
|