[Leetcode] Combinations

来源:互联网 发布:北京人在纽约 知乎 编辑:程序博客网 时间:2024/05/01 08:38
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> cache;        comb(n, k, 0, 1, cache, res);                return res;    }        void comb(int n, int k, int curCount, int curIdx, vector<int> cache, vector<vector<int> >& res)    {                    if (curCount == k)         {            res.push_back(cache);            return;        }                if (curIdx > n) return;                cache.push_back(curIdx);                    comb(n, k, curCount + 1, curIdx + 1, cache, res);        cache.pop_back();        comb(n, k, curCount, curIdx + 1, cache, res);    }};