fork download
#include <iostream>
#include <vector>
using namespace std;

class Solution {
 public:
  void helper(const vector<int>& curr_list, int target,
              const vector<int>& candidates, int start_index,
              vector<vector<int>>* res) {
    if (curr_list.size() == 3) {
      if (target == 0) {
        res->push_back(curr_list);
      }
      return;
    }

    for (int i = start_index; i < candidates.size(); i++) {
      int diff = target - candidates[i];
      auto new_vec = curr_list;
      new_vec.push_back(candidates[i]);
      helper(new_vec, diff, candidates, i, res);
    }
  }

  vector<vector<int>> combinationSum(vector<int>& candidates) {
    vector<vector<int>> res;
    helper(vector<int>(), 0, candidates, 0, &res);
    return res;
  }
};

int main() {
	// your code goes here
	Solution s;
	vector<int> a {-5, 1, 10, 2, 3};
	vector<vector<int>> res= s.combinationSum(a);
	for (auto i: res) 
	{
	 for (auto j : i)
	   cout << j <<endl;
	  cout << "----" <<endl;
	}
	return 0;
}
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
-5
-5
10
----
-5
2
3
----