-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.cpp
More file actions
40 lines (35 loc) · 868 Bytes
/
Combinations.cpp
File metadata and controls
40 lines (35 loc) · 868 Bytes
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
/**
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 combineUtil(vector<vector<int> >& ret, vector<int> combination, int n,
int k, int level) {
if (combination.size() == k) {
ret.push_back(combination);
return ;
}
for (int i = level; i <=n; ++i) {
combination.push_back(i);
combineUtil(ret, combination, n, k,i+1);
combination.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
vector<int> combination;
int level = 1;
combineUtil(ret, combination, n, k, level);
return ret;
}
};