zoukankan      html  css  js  c++  java
  • 【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;
        }
    };

  • 相关阅读:
    OpenIOC
    网站舆情监测
    乌云的背后是阳光
    2014 十大工具
    NetFlow网络流量监测技术的应用和设计(转载)
    免费工具
    Oracle RAC环境下怎样更新patch(Rolling Patch)
    Answer&#39;s Question about pointer
    cocos2d-x 3.0 final 移植 android
    ReactNavtive框架教程(3)
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4149003.html
Copyright © 2011-2022 走看看