-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path216.cpp
More file actions
41 lines (35 loc) · 899 Bytes
/
216.cpp
File metadata and controls
41 lines (35 loc) · 899 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
30
31
32
33
34
35
36
37
38
39
40
41
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
vector<vector<int>> result;
void combinationSum3(int num, int k, int n, vector<int>& tempRes, int sum)
{
if(k == 0 && sum == n)
{
result.push_back(tempRes);
return;
}
for(int i = num; i < 10; i++)
{
if(i + sum <= n && k)
{
tempRes.push_back(i);
combinationSum3(i+1, k-1, n, tempRes, sum+i);
tempRes.pop_back();
}
else
return;
}
}
public:
vector<vector<int>> combinationSum3(int k, int n) {
if(!k)
return result;
vector<int> tempRes;
tempRes.reserve(k);
combinationSum3(1, k, n, tempRes, 0);
return result;
}
};