forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.cpp
More file actions
35 lines (29 loc) · 747 Bytes
/
Combinations.cpp
File metadata and controls
35 lines (29 loc) · 747 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
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> ans;
vector<int> nums;
vector<int> comb;
for(int i=1;i<=n;i++)
{
nums.push_back(i);
}
combine(k,0,ans,nums,comb);
return ans;
}
void combine(int k, int idx,vector<vector<int>> &ans, vector<int> &nums, vector<int> &comb)
{
if(comb.size()==k)
{
ans.push_back(comb);
return;
}
for(int i=idx;i<nums.size();i++)
{
comb.push_back(nums[i]);
combine(k,i+1,ans,nums,comb);
comb.pop_back();
}
return;
}
};