leetcode官方解法实在是太绝了!!二刷的时候一定用上!
/**
* 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 temp[40];
void dfs(int* nums,int numsSize,int** array,int* returnSize,int* column,int index,int count){
if(index>numsSize) return;
column[*returnSize]=count;
array[*returnSize]=(int*)malloc(sizeof(int)*(count+1));
for(int i=0;i<count;i++) array[*returnSize][i]=temp[i];
(*returnSize)++;
for(int i=index;i<numsSize;i++){
temp[count]=nums[i];
dfs(nums,numsSize,array,returnSize,column,i+1,count+1);
temp[count]=0;
}
}
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
*returnSize=0;
int* column=(int*)malloc(sizeof(int)*3000);
int** array=(int**)malloc(sizeof(int*)*3000);
dfs(nums,numsSize,array,returnSize,column,0,0);
*returnColumnSizes=column;
return array;
}
结果:
标签:count,numsSize,returnSize,int,index,子集,array,78 From: https://www.cnblogs.com/llllmz/p/18062841