forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 39
/
combinationSum.III.cpp
88 lines (76 loc) · 2.33 KB
/
combinationSum.III.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Source : https://leetcode.com/problems/combination-sum-iii/
// Author : Hao Chen
// Date : 2015-06-11
/**********************************************************************************
*
* Find all possible combinations of k numbers that add up to a number n,
* given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
* Ensure that numbers within the set are sorted in ascending order.
*
* Example 1:
* Input: k = 3, n = 7
* Output:
*
* [[1,2,4]]
*
* Example 2:
* Input: k = 3, n = 9
* Output:
*
* [[1,2,6], [1,3,5], [2,3,4]]
*
* Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.
*
**********************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
void combinationSumHelper(vector<int>& candidates, int start, int num, int target,
vector<int>& solution, vector< vector<int> >& results) {
if (target < 0|| solution.size() > num){
return;
}
if (target == 0 && solution.size()==num){
results.push_back(solution);
return;
}
for (int i=start; i<candidates.size(); i++){
solution.push_back(candidates[i]);
combinationSumHelper(candidates, i+1, num, target-candidates[i], solution, results);
solution.pop_back();
}
}
vector< vector<int> > combinationSum3(int k, int n) {
vector<int> v;
for (int i=1; i<=9; i++){
v.push_back(i);
}
vector< vector<int> > result;
vector<int> solution;
combinationSumHelper(v, 0, k, n, solution, result);
return result;
}
void PrintResults(int k, int n, vector< vector<int> >& results) {
cout << "k=" << k << ", n=" << n << "\t";
for (int i=0; i<results.size(); i++){
cout << "[";
for (int j=0; j<results[i].size(); j++){
cout << results[i][j] << (j!=results[i].size()-1 ? ",":"]") ;
}
cout << ( i!=results.size()-1 ? ", " : "" );
}
cout << endl;
}
int main(int argc, char** argv)
{
int k = 3, n =9;
if (argc > 2){
k = atoi(argv[1]);
n = atoi(argv[2]);
}
vector< vector<int> > results;
results = combinationSum3(k, n);
PrintResults(k,n, results);
return 0;
}