美文网首页
4.Combinations

4.Combinations

作者: Anaven | 来源:发表于2017-01-02 11:34 被阅读0次

    https://leetcode.com/problems/combinations/

    class Solution {
    public:
        vector<vector<int>> combine(int n, int k) {
            vector<vector<int>> rst;
            vector<int> sol;
            combine(n, k, 1, sol, rst);
            return rst;
        }
        
        void combine(int n, int k, int start, vector<int> &sol, vector<vector<int>> &rst) {
            if (sol.size() == k) {
                rst.push_back(sol);
                return;
            }
            
            for (int i = start; i <= n; i++) {
                sol.push_back(i);
                combine(n, k, i+1, sol, rst);
                sol.pop_back();
            }
        }
    };
    

    相关文章

      网友评论

          本文标题:4.Combinations

          本文链接:https://www.haomeiwen.com/subject/hhjwvttx.html