Combinations

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],
]

解法一:递归

递推点:加入i后,下一个加入的元素需要遍历i+1~n

因此可以基于k做递归。

base case: k==cur.size(),此时cur即为符合条件的一个集合。

class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, k, , n);
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> cur, int k, int pos, int n)
{
if(cur.size() == k)
ret.push_back(cur);
else
{
for(int i = pos; i <= n; i ++)
{
cur.push_back(i);
Helper(ret, cur, k, i+, n);
cur.pop_back();
}
}
}
};

【LeetCode】77. Combinations (2 solutions)-LMLPHP

解法二:非递归

遍历子集过程中,大小为k的子集即为所需集合。

注意略去大小超过k的子集,若不然存储所有子集需要2^n空间。

子集遍历法参考Subsets

class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
vector<vector<int> > subsets;
vector<int> cur; //empty set
//check all subsets with k elements
subsets.push_back(cur);
for(int i = ; i <= n; i ++)
{//all element put into all subsets in ret
int size = subsets.size();
for(int j = ; j < size; j ++)
{
cur = subsets[j];
if(cur.size() >= k)
continue;
cur.push_back(i);
if(cur.size() == k)
ret.push_back(cur);
else
//cur.size() < k
subsets.push_back(cur);
}
}
return ret;
}
};

【LeetCode】77. Combinations (2 solutions)-LMLPHP

04-14 19:31