【LeetCode】77. Combinations (2 solutions)

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, 1, 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+1, n);
                cur.pop_back();
            }
        }
    }
};

 

解法二:非递归

遍历子集过程中,大小为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 = 1; i <= n; i ++)
        {//all element put into all subsets in ret
            int size = subsets.size();
            for(int j = 0; 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;
    }
};

posted @ 2014-12-07 00:01  陆草纯  阅读(391)  评论(0编辑  收藏  举报