脑残了,参数传错了,debug了半天。
/**
* 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];
void dfs(int** array,int* candidates,int candidatesSize,int target,int* returnSize,int index,int sum,int count,int* column){
if(index>candidatesSize || sum>target) 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;
}
temp[count]=candidates[index];
dfs(array,candidates,candidatesSize,target,returnSize,index,sum+candidates[index],count+1,column);
temp[count]=0;
dfs(array,candidates,candidatesSize,target,returnSize,index+1,sum,count,column);
}
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
printf("%d",candidatesSize);
*returnSize=0;
int** array=(int**)malloc(sizeof(int*)*500);
int* column=(int*)malloc(sizeof(int)*500);
dfs(array,candidates,candidatesSize,target,returnSize,0,0,0,column);
*returnColumnSizes=column;
return array;
}
标签:39,target,组合,returnSize,int,sum,candidatesSize,array,总和
From: https://www.cnblogs.com/llllmz/p/18075561