forked from Hearen/AlgorithmHackers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.c
More file actions
47 lines (47 loc) · 1.45 KB
/
Subsets.c
File metadata and controls
47 lines (47 loc) · 1.45 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
/*******************************************
Author: LHearen
E-mail: [email protected]
Time : 2015-12-04 09:05
Description:
Source: https://leetcode.com/problems/subsets/
*******************************************/
//*returnSize will be halfed after each iteration;
//index is used to follow the position in nums;
//setIndex is used to follow the position in allSets;
void combination(int* nums, int numsSize, int** columnSizes, int* returnSize, int **allSets, int index, int setIndex)
{
if(index == numsSize)
{
columnSizes[returnSize] = setIndex;
return;
}
for(int i = 0; i < *returnSize; i++)
{
if(i < *returnSize/2)
{
combination(nums, numsSize, columnSizes, *returnSize/2, allSets, index + 1, setIndex + 1);
allSet[i] = nums[index];
}
else
{
combination(nums, numsSize, columnSizes, *returnSize/2, allSets, index + 1, setIndex);
}
}
}
int **subsets(int* nums, int numsSize, int** columnSizes, int* returnSize)
{
int total = 1;
int count = 0;
while(count < numsSize)
{
total *= 2;
count++;
}
columnSizes = (int*)malloc(sizeof(int*) * total);
*returnSize = total;
int allSets[][] = (int*)malloc(sizeof(int*) * total);
for(int i = 0; i < total; i++)
allSets[i] = (int*)malloc(sizeof(int) * numsSize);
combination(nums, numsSize, columnSizes, returnSize, allSet, 0, 0);
return allSets;
}