-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination_sum3.cpp
More file actions
78 lines (73 loc) · 2.08 KB
/
combination_sum3.cpp
File metadata and controls
78 lines (73 loc) · 2.08 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* =====================================================================================
*
* Filename: combination_sum3.cpp
*
* Description: 216. Combination Sum III. Find all possible combinations of k
* numbers that add up to a number n, given that only numbers from 1 to
* 9 can be used and each combination should be a unique set of numbers.
*
* Version: 1.0
* Created: 03/28/19 02:22:17
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
class Solution
{
public:
std::vector<std::vector<int>> combinationSum3(int k, int n)
{
std::vector<int> candidates = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<std::vector<int>> combx;
std::vector<int> current;
combine(candidates, 0, k, n, ¤t, &combx);
return combx;
}
private:
void combine(const std::vector<int>& candidates, int start, int count, int remain,
std::vector<int>* current, std::vector<std::vector<int>>* combx)
{
if (remain == 0)
{
if (current->size() > 0 && current->size() == count)
{
combx->push_back(*current);
}
return;
}
else if (remain < 0)
{
return;
}
for (int i = start; i < candidates.size(); i++)
{
current->push_back(candidates[i]);
combine(candidates, i + 1, count, remain - candidates[i], current, combx);
current->pop_back();
}
}
};
int main(int argc, char* argv[])
{
int k = 3;
int n = 9;
auto combx = Solution().combinationSum3(k, n);
printf("Number of combinations: %lu\n", combx.size());
for (const auto& item: combx)
{
for (auto num: item)
{
printf("%d ", num);
}
printf("\n");
}
return 0;
}