码迷,mamicode.com
首页 > 其他好文 > 详细

Combinations

时间:2014-05-08 02:10:56      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:combination   leetcode   dfs   

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,布布扣,bubuko.com

Combinations

标签:combination   leetcode   dfs   

原文地址:http://blog.csdn.net/icomputational/article/details/25254519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!