-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path78. Subsets.c
91 lines (71 loc) · 1.79 KB
/
78. Subsets.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
78. Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
*/
/**
* 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().
*/
typedef struct {
int **p;
int *csz;
int psz;
int pn;
} res_t;
void add2res(res_t *res, int *buff, int d) {
if (d) {
int *tmp = malloc(d * sizeof(int));
//assert(tmp);
memcpy(tmp, buff, d * sizeof(int));
res->p[res->pn] = tmp;
} else {
res->p[res->pn] = NULL;
}
res->csz[res->pn ++] = d;
}
void bt(int *nums, int sz, int start, res_t *res, int *buff, int d) {
int i;
add2res(res, buff, d);
for (i = start; i < sz; i ++) {
buff[d] = nums[i];
bt(nums, sz, i + 1, res, buff, d + 1);
}
}
int** subsets(int* nums, int numsSize, int** columnSizes, int* returnSize) {
res_t res = { 0 };
int *buff, i;
res.psz = 1 << numsSize;
res.p = malloc(res.psz * sizeof(int *));
res.csz = malloc(res.psz * sizeof(int));
//assert(res.p && res.csz);
buff = malloc(numsSize * sizeof(int));
//assert(buff);
bt(nums, numsSize, 0, &res, buff, 0);
free(buff);
*columnSizes = res.csz;
*returnSize = res.pn;
return res.p;
}
/*
Difficulty:Medium
Total Accepted:173.7K
Total Submissions:428.5K
Companies Amazon Uber Facebook
Related Topics Array Backtracking Bit Manipulation
Similar Questions
Generalized Abbreviation
*/