Skip to content

Commit 4b4d1f2

Browse files
committed
Create: 0077-combinations.cpp
1 parent 7d94bf5 commit 4b4d1f2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

cpp/0077-combinations.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
private:
3+
void backtrack(int start, int n, int k, vector<int> &combination, vector<vector<int>> &res){
4+
//base case, when size of combination is k, we wanna stop
5+
if(combination.size() == k){
6+
res.push_back(combination);
7+
return;
8+
}
9+
10+
for(int i = start; i<=n; i++){
11+
combination.push_back(i);
12+
backtrack(i+1, n, k, combination, res);
13+
combination.pop_back();
14+
}
15+
}
16+
public:
17+
vector<vector<int>> combine(int n, int k) {
18+
vector<vector<int>> res;
19+
20+
//initial empty list to pass to the backtrack function
21+
vector<int> emptyCombination;
22+
23+
backtrack(1, n, k, emptyCombination, res);
24+
25+
return res;
26+
}
27+
};

0 commit comments

Comments
 (0)