-
Notifications
You must be signed in to change notification settings - Fork 481
/
0216.cpp
34 lines (33 loc) · 895 Bytes
/
0216.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
#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>> combinationSum3(int k, int n)
{
vector<vector<int>> res;
vector<int> path;
_combinationSum3(n, 1, path, res, k);
return res;
}
private:
void _combinationSum3(int target, int index, vector<int>& path, vector<vector<int>>& res, int k)
{
if (target == 0 and path.size() == k)
{
res.push_back(path);
return;
}
if (!path.empty() and target < path.back()) return;
for (unsigned int i = index; i < 10; ++i)
{
path.push_back(i);
_combinationSum3(target - i, i + 1, path, res, k);
path.pop_back();
}
}
};