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);
}
};
[Leetcode] Combinations
最新推荐文章于 2021-02-24 07:35:30 发布