Skip to content

Commit 9e49f88

Browse files
committed
1
1 parent 3a18c12 commit 9e49f88

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Combinations/Combinations.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<vector<int> > combine(int n, int k) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
vector<vector<int>> result;
8+
vector<int> comb;
9+
DFS(comb, 0, 0, k, n, result);
10+
return move(result);
11+
}
12+
13+
void DFS(vector<int>& comb, int step, int p, int k, int n, vector<vector<int>>& result) {
14+
if (step == k) {
15+
result.push_back(comb);
16+
return;
17+
}
18+
if (n - p < k - step)
19+
return;
20+
21+
comb.push_back(p + 1);
22+
DFS(comb, step + 1, p + 1, k, n, result);
23+
comb.pop_back();
24+
25+
DFS(comb, step, p + 1, k, n, result);
26+
}
27+
};

0 commit comments

Comments
 (0)