-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL77.cpp
38 lines (38 loc) · 955 Bytes
/
L77.cpp
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
#include <iostream>
#include <vector>
using namespace std;
class Solution{
public:
vector<vector<int>> res;
vector<int> path;
void dfs(int n, int k, int startindex){
if (path.size() == k){
res.push_back(path);
return;
}
for (int i = startindex; i <= n - (k - path.size()) + 1; i++){ // !这里从startindex开始,避免在数组中选取不重复数字时结果集中出现[2,4]和[4,2]重复的情况
path.push_back(i);
dfs(n, k, i + 1);
path.pop_back();
}
}
vector<vector<int>> combine(int n, int k){
dfs(n, k, 1);
return res;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
Solution sol;
sol.combine(n, k);
for (auto& x : sol.res){
for (auto y : x){
cout << y << " ";
}
cout << "\n";
}
return 0;
}