forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0077-combinations.cpp
More file actions
27 lines (23 loc) · 794 Bytes
/
0077-combinations.cpp
File metadata and controls
27 lines (23 loc) · 794 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
class Solution {
private:
void backtrack(int start, int n, int k, vector<int> &combination, vector<vector<int>> &res){
//base case, when size of combination is k, we wanna stop
if(combination.size() == k){
res.push_back(combination);
return;
}
for(int i = start; i<=n; i++){
combination.push_back(i);
backtrack(i+1, n, k, combination, res);
combination.pop_back();
}
}
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
//initial empty list to pass to the backtrack function
vector<int> emptyCombination;
backtrack(1, n, k, emptyCombination, res);
return res;
}
};