-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubsets II
85 lines (58 loc) · 2.26 KB
/
Subsets II
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
78
79
80
#define RESULT_SIZE 10000
void sort(int* nums, int size) {
int tmp;
for (int i = 1; i < size; i++) {
for (int j = i; j > 0; j--) {
if (nums[j] < nums[j - 1]) {
tmp = nums[j];
nums[j] = nums[j - 1];
nums[j - 1] = tmp;
}
}
}
}
void backtrace(int** result, int* result_ind, int* curr_result, int curr_index, int set_size, int* nums, int numsSize, int start, int* returnColumnSizes) {
if (set_size == 0) {
result[*result_ind] = malloc(sizeof(int) * curr_index);
memcpy(result[*result_ind], curr_result, curr_index * sizeof(int));
returnColumnSizes[*result_ind] = curr_index;
*result_ind = *result_ind + 1;
return;
}
for (int i = start; i < numsSize; i++) {
//if (i == 0 || i != start && (nums[i - 1] != nums[i])) {
// think conditions again
if (i == start || i != start && (nums[i - 1] != nums[i])) {
curr_result[curr_index] = nums[i];
backtrace(result, result_ind, curr_result, curr_index + 1, set_size - 1, nums, numsSize, i + 1, returnColumnSizes);
}
}
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
int** result;
int* curr_result;
int result_index;
(*returnColumnSizes) = malloc(RESULT_SIZE * sizeof(int));
curr_result = malloc(numsSize * sizeof(int));
result = malloc(RESULT_SIZE * sizeof(int*));
*returnSize = 0;
result_index = 0;
if (numsSize == 0) {
*returnSize = 1;
(*returnColumnSizes)[0] = 0;
return result;
}
sort(nums, numsSize);
for (int curr_size = 1; curr_size <= numsSize; curr_size++) {
//printf("%d\n", curr_size);
backtrace(result, &result_index, curr_result, 0, curr_size, nums, numsSize, 0, *returnColumnSizes);
}
(*returnColumnSizes)[result_index] = 0;
*returnSize = result_index+ 1;
return result;
}