-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path39.cpp
32 lines (26 loc) · 804 Bytes
/
39.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> temp;
dfs(0, target, candidates, temp, res);
return res;
}
void dfs(int idx, int _target, vector<int>& candidates, vector<int>& temp, vector<vector<int>>& res) {
if (_target == 0) {
res.emplace_back(temp);
return;
}
int n = candidates.size();
for (int i = idx; i < n; ++i) {
int val = candidates[i];
if (val <= _target) {
temp.push_back(val);
dfs(i, _target-val, candidates, temp, res);
temp.pop_back();
}
}
}
};