Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
class
Solution {
public:
vector<vector<
int> > combine(
int n,
int k)
{
vector<vector<
int>> result;
if(k<=
0)
return result;
vector<
int> v;
for(
int i=
0;i<k;i++) v.push_back(
0);
for(
int i=
1;i<=n;i++)
{
v[
0]=i;
get(result,v,n,k,
1);
}
return result;
}
void
get(vector<vector<
int>>& result,vector<
int>& v,
int n,
int k,
int dep)
{
if(dep==k)
{
result.push_back(v);
return;
}
for(
int i=v[dep-
1]+
1;i<=n;i++)
{
v[dep]=i;
get(result,v,n,k,dep+
1);
}
}
};