forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations.cpp
More file actions
65 lines (54 loc) · 1.29 KB
/
combinations.cpp
File metadata and controls
65 lines (54 loc) · 1.29 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
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
*/
class Solution {
public:
void solve(vector<int> &s, int off, int n, int start, int k, vector<vector<int> > &res) {
if (off == k) {
res.push_back(s);
return ;
}
for (int i = start + 1; i <= n; i++) {
s[off] = i;
solve(s, off + 1, n, i, k, res);
}
}
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > res;
vector<int> s(k);
solve(s, 0, n, 0, k, res);
return res;
}
};
// another way to solve this
class Solution {
public:
void solve(vector<vector<int> > &res, vector<int> &temp, int off, int n, int k) {
if (temp.size() == k) {
res.push_back(temp);
return ;
}
if (off <= n) {
solve(res, temp, off + 1, n, k);
temp.push_back(off);
solve(res, temp, off + 1, n, k);
temp.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > res;
vector<int> temp;
solve(res, temp, 1, n, k);
return res;
}
};