-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.cpp
More file actions
52 lines (50 loc) · 1.37 KB
/
Combinations.cpp
File metadata and controls
52 lines (50 loc) · 1.37 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
class Solution {
public:
void DFS(int now, int id, int size, int num, vector<int> &result, vector<vector<int> > &ans)
{
if(now==size)
{
ans.push_back(result);
return ;
}
for(int i=id;i<=num;i++)
{
result[now]=i;
DFS(now+1, i+1, size, num, result, ans);
}
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > ans;
vector<int> result;
result.resize(k);
DFS(0, 1, k, n, result, ans);
return ans;
}
};
class Solution {
public:
void DFS(int num, int max_num, int depth, vector<int> result, vector<vector<int> > &ans)
{
if(depth == 0)
{
ans.push_back(result);
return ;
}
for(int i = num; i <= max_num; ++i)
{
result.push_back(i);
DFS(i + 1, max_num, depth - 1, result, ans);
result.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result;
vector<vector<int> > ans;
DFS(1, n, k, result, ans);
return ans;
}
};