Combinations

Combinations

  Total Accepted: 10949  Total Submissions: 36507 My 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;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值