/**
* 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[4];
int cmp(const void* a,const void* b){
int x=*(int*)a;
int y=*(int*)b;
if(x>y) return 1;
return -1;
}
void dfs(int** array,int* nums,int numsSize,int* returnSize,int* column,int index,int sum,int count){
if(count>3) return ;
if(index>numsSize) return;
if(index == numsSize && (count!=3 || sum!=0)) return;
if(count==3&&sum!=0 ) return ;
if(count==3 && sum==0){
column[*returnSize]=3;
array[*returnSize]=(int*)malloc(sizeof(int)*3);
for(int i=0;i<3;i++) array[*returnSize][i]=temp[i];
(*returnSize)++;
return;
}
printf("%d ", index);
temp[count]=nums[index];
dfs(array,nums,numsSize,returnSize,column,index+1,sum+nums[index],count+1);
while(index!=numsSize-1 && nums[index] == nums[index+1]) index++;
dfs(array,nums,numsSize,returnSize,column,index+1,sum,count);
}
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
qsort(nums,numsSize,sizeof(int),cmp);
*returnSize=0;
int* column=(int*)malloc(sizeof(int)*50000);
int** array=(int**)malloc(sizeof(int*)*50000);
dfs(array,nums,numsSize,returnSize,column,0,0,0);
*returnColumnSizes=column;
return array;
}
标签:count,15,returnSize,int,三数,sum,return,array From: https://www.cnblogs.com/llllmz/p/18080702