solution.cpp 472 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	vector<vector<int>> combine(int n, int k)
	{
		vector<vector<int>> res;
		dfs(n, k, 1, res);
		return res;
	}
private:
	vector<int> stack;
	void dfs(int n, int k, int start, vector<vector<int>> &res)
	{
		if (stack.size() == k)
		{
			res.push_back(stack);
		}
		else
		{
			for (int i = start; i <= n; i++)
			{
				stack.push_back(i);
				dfs(n, k, i + 1, res);
				stack.pop_back();
			}
		}
	}
};