-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.h
More file actions
52 lines (47 loc) · 1.05 KB
/
Combinations.h
File metadata and controls
52 lines (47 loc) · 1.05 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 : luoshikai
* Version : 1.0
* Date : 2013-10-22
* Email : [email protected]
*************************************/
/**************************************
* 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:
vector<vector<int> > res;
vector<vector<int> > combine(int n, int k)
{
res.clear();
vector<int> com;
combineRe(n, k, com, -1, 0);
return res;
}
void combineRe(int n, int k, vector<int> & com, int last, int deep)
{
if (k == 0)
{
res.push_back(com);
return;
}
for (int i = last+1; i < n; ++i)
{
com.push_back(i+1);
combineRe(n, k-1, com, i, deep+1);
com.pop_back();
}
}
};