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;
        }
    };

  • 相关阅读:
    bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
    luoguP1886 滑动窗口 [单调队列]
    bzoj1047: [HAOI2007]理想的正方形
    bzoj1012: [JSOI2008]最大数maxnumber [单调队列]
    树与二叉树之二--二叉树的性质与存储
    树与二叉树之一--基本概念与存储结构
    Markdown段首空格
    C++ atan2
    凸包学习笔记
    Codeforces Round #545 (Div. 1) E. Train Car Selection
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4149003.html
Copyright © 2011-2022 走看看