-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_77_Combinations.cpp
62 lines (49 loc) · 1.14 KB
/
LC_77_Combinations.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
/*
https://leetcode.com/problems/combinations/
77. Combinations
*/
class Solution {
public:
vector<vector<int>> ans;
int N=0, K=0;
vector<vector<int>> combine(int n, int k) {
N=n;
K=k;
// for(int i=1; i<=N; i++)
// {
// backtrackHelper(i, {i});
// }
vector<int> cur;
combineHelper(k, cur, 1);
return ans;
}//end
void backtrackHelper(int idx, vector<int> temp)
{
if(temp.size() == K)
{
ans.push_back(temp);
}
// temp.push_back(idx);
for(int i=idx+1; i<=N; i++)
{
temp.push_back(i);
backtrackHelper(i, temp);
temp.pop_back();
}
// temp.pop_back();
}
void combineHelper(int k, vector<int> &cur, int i)
{
if(k==0)
{
ans.push_back(cur);
return;
}
for(int j=i; j <= N-k+1; j++)
{
cur.push_back(j);
combineHelper(k-1, cur, j+1);
cur.pop_back();
}
}//
};