Skip to content

Commit b555b2e

Browse files
Create 0039. Code Testcase Testcase Test Result 39. Combination Sum.md
1 parent b9154f1 commit b555b2e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
```cpp
2+
class Solution {
3+
public:
4+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
5+
vector<vector<int>> res;
6+
vector<int> path;
7+
8+
sort(candidates.begin(), candidates.end());
9+
10+
function<void(int, int)> backtrack = [&](int start, int target) {
11+
if(target == 0) {
12+
res.push_back(path);
13+
return;
14+
}
15+
16+
for(int i = start; i < candidates.size(); i++) {
17+
if(candidates[i] > target) break;
18+
path.push_back(candidates[i]);
19+
backtrack(i, target - candidates[i]);
20+
path.pop_back();
21+
}
22+
};
23+
24+
backtrack(0, target);
25+
return res;
26+
}
27+
};
28+
```

0 commit comments

Comments
 (0)