-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path216.cpp
More file actions
29 lines (26 loc) · 806 Bytes
/
Copy path216.cpp
File metadata and controls
29 lines (26 loc) · 806 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
vector<int> temp;
dfs(temp, res, 1, n, 9, k, 0);
return res;
}
void dfs(vector<int> temp, vector<vector<int>> &res, int idx, int target, int n, int k, int depth)
{
if (depth == k) {
if (target == 0) res.emplace_back(temp);
return;
}
for (int i = idx; i <= n; ++i) {
// printf("%d==%d, target-i:%d, i+1:%d \n", i, target, target-i, i+1);
if (target-i >= 0) { // 剪枝
temp.push_back(i);
dfs(temp, res, i+1, target-i, n, k, depth+1);
temp.pop_back();
}
}
}
};