-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSumIII.cpp
More file actions
62 lines (58 loc) · 1.32 KB
/
CombinationSumIII.cpp
File metadata and controls
62 lines (58 loc) · 1.32 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
void print(vector<int> &v){
for(auto i:v){
cout << i << " ";
}
cout << endl;
}
void print(vector<vector<int>> &v){
for(auto &i:v){
print(i);
}
}
class Solution {
public:
void combinationSum3(vector<vector<int>> &ret, int k, int n, int start){
if(k <= 0)
return;
if(k == 1){
if(n<=9 && n>start){
vector<int> tmp;
tmp.push_back(n);
ret.push_back(tmp);
}
} else if(k == n && start == 0 && n==1){
vector<int> tmp;
tmp.push_back(1);
ret.push_back(tmp);
} else if(k>n){
;
} else{
for(int i=start+1; i<=9; i++){
vector<vector<int>> tmp;
combinationSum3(tmp, k-1, n-i, i);
for(vector<int> &v : tmp){
v.push_back(i);
ret.push_back(v);
}
}
}
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ret;
combinationSum3(ret, k, n, 0);
return ret;
}
};
int main(int argc, char *argv[]) {
Solution sol;
vector<vector<int>> ret = sol.combinationSum3(3, 9);
print(ret);
return 0;
}