/**
* 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[100];
int cmp(const void* a,const void* b){
return *(int*)a-*(int*)b;
}
void dfs(int** array,int* candidates,int candidatesSize,int target,int* returnSize,int* column,int index,int sum,int count){
if(sum>target||index>candidatesSize) return;
if(index==candidatesSize&&sum!=target) return;
if(sum==target){
column[*returnSize]=count;
array[*returnSize]=(int*)malloc(sizeof(int)*count);
for(int i=0;i<count;i++) array[*returnSize][i]=temp[i];
(*returnSize)++;
return;
}
int use[51]={0};
for(int i=index;i<candidatesSize;i++){
if(use[candidates[i]]!=0) continue;
temp[count]=candidates[i];
use[candidates[i]]=1;
dfs(array,candidates,candidatesSize,target,returnSize,column,i+1,sum+candidates[i],count+1);
}
}
int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
*returnSize=0;
int** array=(int**)malloc(sizeof(int*)*2000);
int* column=(int*)malloc(sizeof(int)*2000);
qsort(candidates,candidatesSize,sizeof(int),cmp);
dfs(array,candidates,candidatesSize,target,returnSize,column,0,0,0);
*returnColumnSizes=column;
return array;
}
标签:target,returnSize,int,sum,40,candidatesSize,IIc,array,总和
From: https://www.cnblogs.com/llllmz/p/18075618