class Solution {
private:
vector<vector<int> > res;
vector<int> a;
public:
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
a.resize(k);
res.clear();
dfs(0, k, n, 1);
return res;
}
void dfs(int idx, int k, int n, int start)
{
if (idx == k)
{
res.push_back(a);
return;
}
for (int i = start; i <= n; i++)
{
a[idx] = i;
dfs(idx + 1, k, n, i + 1);
}
}
};
[Leetcode] Combination
最新推荐文章于 2019-05-21 16:08:00 发布