Combinations

Posted phlsheji

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
};


以上是关于Combinations的主要内容,如果未能解决你的问题,请参考以下文章

个人记录-LeetCode 77. Combinations

个人记录-LeetCode 77. Combinations

leetcode-Combinations-77

[Lintcode]152. Combinations/[Leetcode]77. Combinations

LeetCode(17)Letter Combinations of a Phone Number

leetcode17. Letter Combinations of a Phone Number