-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.cpp
More file actions
42 lines (40 loc) · 920 Bytes
/
Copy pathCombinations.cpp
File metadata and controls
42 lines (40 loc) · 920 Bytes
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
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
if(n==0||k==0||n<k)
return vector<vector<int> >();
vector<vector<int> > ret;
vector<int> path;
combinehelper(n,k,1,ret,path);
return ret;
}
void combinehelper(int n,int k,int start,vector<vector<int> > &ret,vector<int> &path)
{
if(k==0)
{
ret.push_back(path);
return;
}
int i;
for(i=start;i<=n;i++)
{
path.push_back(i);
combinehelper(n,k-1,i+1,ret,path);
path.pop_back();
}
}
};
int main()
{
Solution s;
vector<vector<int> > result=s.combine(1,2);
for(auto a:result)
{
for(auto v:a)
cout<<v<<" ";
cout<<endl;
}
}