152. 组合
给定两个整数 n 和 k. 返回从 1, 2, ... , n 中选出 k 个数的所有可能的组合.
样例
样例 1:
输入: n = 4, k = 2
输出: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
样例 2:
输入: n = 4, k = 1
输出: [[1],[2],[3],[4]]
vector<vector<int>> combine(int n, int k)
{
set<set<int>> retSet;
set<set<int>> tmpSet;
for (int i = 1; i <= n; i++)
{
for (auto it :tmpSet)
{
set<int> tmp2 = it;
tmp2.insert(i);
if (k == tmp2.size())
{
retSet.insert(tmp2);
}
else
{
tmpSet.insert(tmp2);
}
}
set<int> tmp3;
tmp3.insert(i);
if (k == tmp3.size())
{
retSet.insert(tmp3);
}
else
{
tmpSet.insert(tmp3);
}
}
vector<vector<int>>retVec;
for (auto it : retSet)
{
vector<int>tmpVec;
for (auto itt : it)
{
tmpVec.push_back(itt);
}
retVec.push_back(tmpVec);
}
return retVec;
}