• Combinations


    Combinations

     Total Accepted: 10949 Total Submissions: 36507My Submissions

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


    解法1。2: DFS遍历全部组合。注意推断当前cur的长度以及当前能够值的状态能否够继续
    解法3,4:用next(prev) permutation来实现获取全部组合
    解法5: 类似非递归的DFS(或者能够想象成为整数逐步加1进位,遍历全部可能数值)来编译状态
    解法6:长度从1到k,每次把全部长度为i- 1的已有组合遍历一次。取全部可能的组合形成长度为i的全部组合(题目要求从小到大。这也为我们这样实现提供的根据)



    class Solution {
    public:
        vector<vector<int> > combine(int n, int k) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            vector<vector<int>> ret;
            if (k < 1 || k > n) return ret;
            
            // 1
            /*
            vector<int> cur;
            genCombination1(ret, cur, n, k, 1);
            */
            
            // 2
            /*
            vector<int> cur;
            genCombination2(ret, cur, n, k, 1);
            */
            
            // 3
            /*
            vector<int> flag(n, 0);
            for (int i = 0; i < k; ++i) {
                flag[i] = 1;
            }
            
            do {
                ret.push_back(vector<int>());
                auto it = back_inserter(ret.back());
                for (int i = 0; i < n; ++i) {
                    if (flag[i] == 1) it = i + 1;
                }
            //} while (prev_permutation(flag.begin(), flag.end()));
            } while (m_prev_permutation(flag));
            */
            
            // 4
            /*
            vector<int> flag(n, 0);
            for (int i = n - k; i < n; ++i) {
                flag[i] = 1;
            }
            
            do {
                ret.push_back(vector<int>());
                auto it = back_inserter(ret.back());
                for (int i = 0; i < n; ++i) {
                    if (flag[i] == 1) it = i + 1;
                }
            //} while (next_permutation(flag.begin(), flag.end()));
            } while (m_next_permutation(flag));
            */
            
            // 5
            /*
            vector<int> lo(k, 0), hi(k, 0);
            for (int i = 1; i <= k; ++i) {
                lo[i - 1] = i;
                hi[i - 1] = n - k + i;
            }
            
            while (true) {
                ret.push_back(lo);
                int cpos = k - 1;
                while (cpos >= 0) {
                    if (lo[cpos] < hi[cpos]) {
                        ++lo[cpos];
                        for (int i = cpos + 1; i < k; ++i) {
                            lo[i] = lo[i - 1] + 1;
                        }
                        break;
                    }
                    
                    --cpos;
                }
                
                if (cpos < 0) break;
            }
            */
            
            // 6
            for (int i = 1; i <= n - k + 1; ++i) {
                ret.push_back(vector<int>(1, i));
            }
            
            for (int i = 2; i <= k; ++i) {
                int cnt = ret.size();
                for (int j = 0; j < cnt; ++j) {
                    int cval = ret[j].back();
                    int lastval = n - k + i;
                    for (int t = cval + 1; t < lastval; ++t) {
                        ret.push_back(ret[j]);
                        ret.back().push_back(t);
                    }
                    
                    ret[j].push_back(lastval);
                }
            }
            
            return ret;
        }
        
    private:
        void genCombination1(vector<vector<int>> &ret, vector<int> &cur, int n, int k, int cval) {
            if (n - cval + 1 < k - cur.size()) return;
            if (cur.size() == k) {
                ret.push_back(cur);
                return;
            }
            
            cur.push_back(cval);
            genCombination1(ret, cur, n, k, cval + 1);
            cur.pop_back();
            genCombination1(ret, cur, n, k, cval + 1);
        }
        
        void genCombination2(vector<vector<int>> &ret, vector<int> &cur, int n, int k, int cval) {
            int cnt = cur.size();
            if (cnt == k) {
                ret.push_back(cur);
                return;
            }
            
            
            for (int i = cval; i <= n - k + cnt + 1; ++i) {
                cur.push_back(i);
                genCombination2(ret, cur, n, k, i + 1);
                cur.pop_back();
            }
        }
        
        bool m_prev_permutation(vector<int> &f) {
            for (int i = f.size() - 1; i > 0; --i) {
                if (f[i] >= f[i - 1]) continue;
                int spos = i;
                for (int j = spos + 1; j < f.size(); ++j) {
                    if (f[j] >= f[spos] && f[j] < f[i - 1])
                        spos = j;
                }
                
                swap(f[spos], f[i - 1]);
                reverse(f.begin() + i, f.end());
                return true;
            }
            
            return false;
        }
        
        bool m_next_permutation(vector<int> &f) {
            for (int i = f.size() - 1; i > 0; --i) {
                if (f[i] <= f[i - 1]) continue;
                int spos = i;
                for (int j = spos + 1; j < f.size(); ++j) {
                    if (f[j] > f[i - 1] && f[j] <= f[spos])
                        spos = j;
                }
                
                swap(f[spos], f[i - 1]);
                reverse(f.begin() + i, f.end());
                return true;
            }
            
            return false;
        }
    };
    


  • 相关阅读:
    Mysql 5.6 源码编译安装
    Python中的数据类型之字符串
    Python中变量的命名与使用(个人总结)
    Windows环境下python3.7版本怎么安装pygame
    web应用无法访问的原因之一以及如何设置数据库编码
    项目代码设计规范总结之分页查询
    当java web项目部署到服务器上时,无法将图片等媒体文件保存到服务器的最终奥义
    如何查看服务器上打印的信息
    java代码实现JVM栈溢出,堆溢出
    springmvc源码分析(转)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5326619.html
Copyright © 2020-2023  润新知