组合问题
给出两个整数n和k,返回从1到n中取k个数字的所有可能的组合
class Solution {
public:
/**
*
* @param n int整型
* @param k int整型
* @return int整型vector<vector<>>
*/
void backTracking(int n,int deepth,int k,vector<int> path,vector<vector<int> >& v)
{
int i;
if(path.empty())
{
i=1;
}
else
{
i=path.back()+1;
}
for(;i<=n;++i)
{
path.push_back(i);
if(deepth==k)
{
v.push_back(path);
path.pop_back();
}
else
{
backTracking(n,deepth+1,k,path,v);
path.pop_back();
}
}
}
vector<vector<int> > combine(int n, int k)
{
// write code here
if(n==0)
{
return vector<vector<int>>();
}
else
{
vector<vector<int> > v;
vector<int> path;
backTracking(n,1,k,path,v);
return v;
}
}
};
};
