forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_dfs_n!.cpp
More file actions
53 lines (48 loc) · 1.13 KB
/
Copy pathAC_dfs_n!.cpp
File metadata and controls
53 lines (48 loc) · 1.13 KB
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_n!.cpp
* Create Date: 2014-12-29 11:21:35
* Descripton: use dfs to generate combinations
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
void dfs(vector<vector<int> > &res, vector<int> &v,
int cur, int rest, int n) {
if (rest == 0) {
res.push_back(v);
return;
}
v.push_back(0);
int len = v.size();
for (int i = cur; i <= n - rest + 1; i++) {
v[len - 1] = i;
dfs(res, v, i + 1, rest - 1, n);
}
v.pop_back();
}
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > res;
if (n < k || !n || !k)
return res;
vector<int> v;
dfs(res, v, 1, k, n);
return res;
}
};
int main() {
int n, k;
Solution s;
while (cin >> n >> k) {
vector<vector<int> > res = s.combine(n, k);
for (auto &i : res) {
for (auto &j : i)
cout << j << ' ';
cout << endl;
}
}
return 0;
}