leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0039.cpp (681B)


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