forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0077.cpp
35 lines (33 loc) · 860 Bytes
/
0077.cpp
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
31
32
33
34
35
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<vector<int>> combine(int n, int k)
{
vector<vector<int>> result;
if (n <= 0 or k <= 0 or n < k) return result;
vector<int> nums;
_combine(n, k, 1, nums, result);
return result;
}
private:
void _combine(int n, int k, int start, vector<int> nums, vector<vector<int>>& result)
{
if (k == nums.size())
{
result.push_back(nums);
return;
}
for (int i = start; i <= n - (k - nums.size()) + 1; ++i)
{
nums.push_back(i);
_combine(n, k, i + 1, nums, result);
nums.pop_back();
}
}
};