forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0216.cpp
36 lines (35 loc) · 1.08 KB
/
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
35
36
#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<int> nums = {1,2,3,4,5,6,7,8,9};
vector<vector<int>> res;
vector<int> path;
_combinationSum3(nums, n, 0, path, res, k);
return res;
}
private:
void _combinationSum3(vector<int>& candidates, 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.end() - 1)) return;
for (unsigned int i = index; i < candidates.size(); ++i)
{
if (i > index and candidates[i] == candidates[i - 1]) continue;
path.push_back(candidates[i]);
_combinationSum3(candidates, target - candidates[i], i + 1, path, res, k);
path.pop_back();
}
}
};