[Leetcode] Combinations

来源:互联网 发布:s71200编程手册 编辑:程序博客网 时间:2024/05/01 18:35
class Solution {public:    vector<vector<int> > combine(int n, int k) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int> > res;        vector<int> buf(k);        build(n, k, 1, 0, res, buf);                return res;    }        void build(int n, int k, int cur, int idx, vector<vector<int> >& res, vector<int>& buf)    {        if (idx == k)        {            res.push_back(buf);            return;        }                for (int i = cur; i <= n; ++i)        {            buf[idx] = i;            build(n, k, i + 1, idx + 1, res, buf);        }    }};

原创粉丝点击