-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.cpp
89 lines (83 loc) · 2.18 KB
/
combine.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
this -> n = n;
this -> k = k;
ans.clear();
comb.clear();
backtracking(1, 1);
return ans;
}
void backtracking(int idx, int cur) {
if (idx > k) {
ans.push_back(comb);
return;
}
for (int i = cur; i <= n - k + idx; i++) {
comb.push_back(i);
backtracking(idx + 1, i + 1);
comb.pop_back();
}
}
private:
vector<vector<int> > ans;
vector<int> comb;
int n, k;
};
class Solution {
private:
vector<vector<int>> result; // 存放符合条件结果的集合
vector<int> path; // 用来存放符合条件结果
int n, k;
void backtracking(int startIndex) {
if (path.size() == k) {
result.push_back(path);
return;
}
for (int i = startIndex; i <= n; i++) {
path.push_back(i); // 处理节点
backtracking(i + 1); // 递归
path.pop_back(); // 回溯,撤销处理的节点
}
}
public:
vector<vector<int>> combine(int n, int k) {
result.clear(); // 可以不写
path.clear(); // 可以不写
this -> n = n;
this -> k = k;
backtracking(1);
return result;
}
};
// 作者:carlsun-2
// 链接:https://leetcode-cn.com/problems/combinations/solution/dai-ma-sui-xiang-lu-dai-ni-xue-tou-hui-s-0uql/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
this -> n = n;
this -> k = k;
ans.clear();
comb.clear();
backtracking(1);
return ans;
}
void backtracking(int cur) {
if (idx > k) {
ans.push_back(comb);
return;
}
int x = comb.size() + 1;
for (int i = cur; i <= n - k + x; i++) {
comb.push_back(i);
backtracking(i + 1);
comb.pop_back();
}
}
private:
vector<vector<int> > ans;
vector<int> comb;
int n, k;
};