forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum.c
75 lines (68 loc) · 2.27 KB
/
combination_sum.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static int compare(const void *a, const void *b)
{
return *(int *) a - *(int *) b;
}
static void dfs(int *nums, int size, int start, int target, int *solution, int len,
bool *used, int **results, int *column_sizes, int *count)
{
int i;
if (target == 0) {
results[*count] = malloc(len * sizeof(int));
memcpy(results[*count], solution, len * sizeof(int));
column_sizes[*count] = len;
(*count)++;
} else if (target > 0) {
for (i = start; i < size; i++) {
if (!used[i]) {
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) continue;
used[i] = true;
solution[len] = nums[i];
dfs(nums, size, i + 1, target - nums[i], solution, len + 1, used, results, column_sizes, count);
used[i] = false;
}
}
}
}
/**
** Return an array of arrays of size *returnSize.
** The sizes of the arrays are returned as *columnSizes array.
** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
**/
static int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize)
{
qsort(candidates, candidatesSize, sizeof(int), compare);
int *solution = malloc(target * sizeof(int));
int **results = malloc(100 * sizeof(int *));
bool *used = malloc(candidatesSize);
memset(used, false, candidatesSize);
*columnSizes = malloc(100 * sizeof(int));
*returnSize = 0;
dfs(candidates, candidatesSize, 0, target, solution, 0, used, results, *columnSizes, returnSize);
return results;
}
int main(int argc, char **argv)
{
if (argc < 3) {
fprintf(stderr, "Usage: ./test target n1 n2...\n");
exit(-1);
}
int i, j, count = 0;
int target = atoi(argv[1]);
int *nums = malloc((argc - 2) * sizeof(int));
for (i = 0; i < argc - 2; i++) {
nums[i] = atoi(argv[i + 2]);
}
int *sizes;
int **lists = combinationSum(nums, argc - 2, target, &sizes, &count);
for (i = 0; i < count; i++) {
for (j = 0; j < sizes[i]; j++) {
printf("%d ", lists[i][j]);
}
printf("\n");
}
return 0;
}