Skip to content

Commit 2806c2f

Browse files
Create combinations.cpp
1 parent d8799db commit 2806c2f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: combinations.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> combine(int n, int k) {
4+
vector<vector<int>> output;
5+
vector<int> combination;
6+
combine(output, combination, n, k, 1);
7+
return output;
8+
}
9+
10+
private:
11+
void combine(vector<vector<int>>& output, vector<int> combination, int n, int k, int i) {
12+
if (combination.size() == k) {
13+
output.push_back(combination);
14+
return;
15+
}
16+
for (int j = i; j <= n; j++) {
17+
combination.push_back(j);
18+
combine(output, combination, n, k, j + 1);
19+
combination.pop_back();
20+
}
21+
}
22+
};

0 commit comments

Comments
 (0)