-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination Sum3.cpp
49 lines (46 loc) · 950 Bytes
/
Combination Sum3.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
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
void cur(vector<vector<int> >& re,vector<int> cur_re,int k,int dep,int index,int sum,int target)
{
if (dep==k)
{
if (sum==target)
{
re.push_back(cur_re);
return;
}
else
return;
}
if (sum>target)
return;
for (int i=index;i<10;i++)
{
cur_re.push_back(i);
cur(re,cur_re,k,dep+1,i+1,sum+i,target);
cur_re.pop_back();
}
}
vector<vector<int> > combinationSum3(int k,int n)
{
vector<vector<int> > re;
vector<int> cur_re;
cur(re,cur_re,k,0,1,0,n);
return re;
}
int main()
{
vector<vector<int> > re;
vector<int> candidates;
re=combinationSum3(3,9);
for (int i=0;i<re.size();i++)
{
for (int j=0;j<re[i].size();j++)
cout<<re[i][j]<<" ";
cout<<endl;
}
return 0;
}