-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinations_77.cpp
More file actions
33 lines (25 loc) · 834 Bytes
/
Combinations_77.cpp
File metadata and controls
33 lines (25 loc) · 834 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
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 77. Combinations
~ Link : https://leetcode.com/problems/combinations/
*/
class Solution {
public:
void combineUtil(int curr, int k, int n, vector<int> &currCombination, vector<vector<int>> &ans) {
if (currCombination.size() == k) {
ans.emplace_back(currCombination);
return;
}
for (int i = curr; i <= n; ++i) {
currCombination.emplace_back(i);
combineUtil(i + 1, k, n, currCombination, ans);
currCombination.pop_back();
}
}
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> ans;
vector<int> currCombination;
combineUtil(1, k, n, currCombination, ans);
return ans;
}
};