forked from mahendrarathore1742/leetcode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode0039-combination-sum.cpp
40 lines (36 loc) · 1.19 KB
/
leetcode0039-combination-sum.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
33
34
35
36
37
38
39
40
/*
* Copyright (C) 2018 all rights reserved.
*
* Author: Houmin Wei <[email protected]>
*
* Source: https://leetcode.com/problems/combination-sum
*
* Description:
* Given a set of candidate numbers(C)(without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
*
* The Same repeated number may be chosen from C unlimited number of times.
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ret;
vector<int> path;
findSum(candidates, target, path, ret, 0);
return ret;
}
void findSum(vector<int>& candidates, int target, vector<int>& path, vector<vector<int>>& ret, int begin) {
if (target == 0) {
ret.push_back(path);
return ;
}
for (int i = begin; i < candidates.size() && target >= candidates[i]; i++) {
path.push_back(candidates[i]);
findSum(candidates, target-candidates[i], path, ret, i);
path.pop_back();
}
}
};