-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.cpp
More file actions
57 lines (52 loc) · 1.14 KB
/
Combinations.cpp
File metadata and controls
57 lines (52 loc) · 1.14 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
/*Question:
Combinations
Asked in:
Amazon
Adobe
Given two integers n and k, return all possible combinations of k numbers out of 1 2 3 ... n.
Make sure the combinations are sorted.
To elaborate,
Within every entry, elements should be sorted. [1, 4] is a valid entry while [4, 1] is not.
Entries should be sorted within themselves.
Example :
If n = 4 and k = 2, a solution is:
[
[1,2],
[1,3],
[1,4],
[2,3],
[2,4],
[3,4],
]
Warning : DO NOT USE LIBRARY FUNCTION FOR GENERATING COMBINATIONS.
Example : itertools.combinations in python.
If you do, we will disqualify your submission retroactively and give you penalty points.
*/
vector<vector<int>> ret;
void foo(int a,int b,int i,vector<int> as)
{
if(i>a)
{
if(b==0){
sort(as.begin(),as.end());
ret.push_back(as);
}
return;
}
if(b==0)
foo(a,b,i+1,as);
else
{
foo(a,b,i+1,as);
as.push_back(i);
foo(a,b-1,i+1,as);
as.pop_back();
}
}
vector<vector<int> > Solution::combine(int A, int B) {
ret.clear();
vector<int> as;
foo(A,B,1,as);
sort(ret.begin(),ret.end());
return ret;
}