Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
public:
void combinedfs(int start,int end,int it,vector<int> &vec,vector<vector<int>> &result)
{
if(it==0)
{
result.push_back(vec);
return ;
}
for(int i=start;i<=end;i++)
{
vec.push_back(i);
combinedfs(i+1,end,it-1,vec,result);
vec.pop_back();
}
}
vector<vector<int>> combine(int n, int k) {
vector<int> vec;
vector<vector<int>> result;
combinedfs(1,n,k,vec,result);
return result;
}
};