【leetcode】78. 子集
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
int** ans = malloc(sizeof(int*) * (1 << numsSize));
*returnColumnSizes = malloc(sizeof(int) * (1 << numsSize));
*returnSize = 1 << numsSize;
int t[numsSize];
for (int mask = 0; mask < (1 << numsSize); ++mask) {
int tSize = 0;
for (int i = 0; i < numsSize; ++i) {
if (mask & (1 << i)) {
t[tSize++] = nums[i];
}
}
int* tmp = malloc(sizeof(int) * tSize);
memcpy(tmp, t, sizeof(int) * tSize);
(*returnColumnSizes)[mask] = tSize;
ans[mask] = tmp;
}
return ans;
}
void recursion(int* nums,int n, int* returnSize, int* col,int* temp,int cnt,int** arr,int start){
for(int i=start; i<n; i++){
temp[cnt]=nums[i];
arr[(*returnSize)]=(int*)calloc(cnt+1,sizeof(int));
memcpy(arr[(*returnSize)],temp,(cnt+1)*sizeof(int));
col[(*returnSize)++]=cnt+1;
recursion(nums, n, returnSize, col, temp, cnt+1, arr,i+1);
}
}
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
int** arr=(int**)calloc((1<<numsSize),sizeof(int*));
int temp[100]={0};
*returnSize=1;
*returnColumnSizes=(int*)calloc((1<<numsSize),sizeof(int));
recursion(nums,numsSize,returnSize,*returnColumnSizes,temp,0,arr,0);
return arr;
}