Time: 10 ms (26.92%), Space: 6.7 MB (23.85%) - LeetHub

This commit is contained in:
Deven
2023-05-25 13:46:15 -04:00
parent cad125d7f3
commit 61822a51f0
+30
View File
@@ -0,0 +1,30 @@
/**
* 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** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
int twoN = pow(2, numsSize);
int **subsets = malloc(twoN * sizeof(int *));
*returnColumnSizes = malloc(twoN * sizeof(int));
for (int i = 0; i < twoN; ++i) {
int iCount = i;
int *array = NULL;
int arraySize = 0;
int j = 0;
while (iCount > 0) {
if (iCount & 1) {
++arraySize;
array = realloc(array, arraySize * sizeof(int));
array[arraySize - 1] = nums[j];
}
iCount >>= 1;
++j;
}
subsets[i] = array;
(*returnColumnSizes)[i] = arraySize;
}
*returnSize = twoN;
return subsets;
}