-
Notifications
You must be signed in to change notification settings - Fork 2
/
039 Combination Sum.cpp
69 lines (64 loc) · 1.65 KB
/
039 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <algorithm>
/*
Combination Sum
https://leetcode.com/problems/combination-sum/
Classic backtracking
There are 3 type of questions, for example, give [1,2,3], and target = 4
If only allowing use each number once, result is:
(1,3)
And CL will be:
recursive(input,sofar,i+1,tag - input[i]);
If allowing use each number infinite times, result is:
(1, 1, 1, 1)
(1, 1, 2)
(1, 3)
(2, 2)
And CL will be:
recursive(input,sofar,i,tag - input[i]);
If allowing use each number infinite times, and different result order is count as differen result:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
And CL will be:
recursive(input,sofar,0,tag - input[i]);
*/
std::vector<std::vector<int> > pool;
void recursive(std::vector<int>& input, std::vector<int>& sofar, int start, int tag)
{
if(tag == 0)
{
pool.push_back(sofar);
}
else if(tag > 0)
{
for(int i = start;i<input.size();i++)
{
if(i>start && input[i] == input[i-1])
continue;
if(input[i] <= tag)
{
sofar.push_back(input[i]);
recursive(input,sofar,i,tag - input[i]); // Critial Line (CL)
sofar.pop_back();
}
}
}
}
std::vector<std::vector<int> > combinationSum(std::vector<int>& input, int tag)
{
pool.clear();
std::stable_sort(input.begin(),input.end());
std::vector<int> sofar;
recursive(input,sofar,0,tag);
return pool;
}
int main()
{
return 0;
}