-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinations.cpp
43 lines (36 loc) · 1.29 KB
/
Combinations.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
/*
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
Link: http://www.lintcode.com/en/problem/combinations/
Example: For example,
If n = 4 and k = 2, a solution is:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]]
Solution: Construct all possible solution, remember pop last reminder.
Source: https://github.com/kamyu104/LintCode/blob/master/C++/combinations.cpp
*/
class Solution {
public:
/**
* @param n: Given the range of numbers
* @param k: Given the numbers of combinations
* @return: All the combinations of k numbers out of 1..n
*/
vector<vector<int> > combine(int n, int k) {
// write your code here
vector<vector<int>> result;
vector<int> ans;
CombinationsHelper(n, k, 1, &ans, &result);
return result;
}
void CombinationsHelper(int n, int k, int start, vector<int>* ans,
vector<vector<int>>* result) {
if (ans->size() == k) {
result->emplace_back(*ans);
return;
}
for (int i = start; i <= n && k - ans->size() <= n - i + 1; ++i) {
ans->emplace_back(i);
CombinationsHelper(n, k, i + 1, ans, result);
ans->pop_back();
}
}
};