/**
* 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* candidates,int candidatesSize,int** array,int target,int* returnSize,int* column,int now,int n){
if(now>target ) return;
if(now==target){
column[*returnSize]=n;
for(int i=0;i<n;i++) array[*returnSize][i]=temp[i];
(*returnSize)++;
return;
}
for(int i=0;i<candidatesSize;i++){
if(n!=0 && temp[n-1] >candidates[i]) continue;
temp[n]=candidates[i];
dfs(candidates,candidatesSize,array,target,returnSize,column,now+candidates[i],n+1);
}
}
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
*returnSize=0;
int** array=(int**)malloc(sizeof(int*)*500);
for(int i=0;i<500;i++) array[i]=(int*)malloc(sizeof(int)*50);
int* column=(int*)malloc(sizeof(int)*500);
dfs(candidates,candidatesSize,array,target,returnSize,column,0,0);
printf("%d",*returnSize);
*returnColumnSizes=column;
return array;
}
多么痛的领悟啊。分配内存的时候分配少了。找了近2个小时错误。。
标签:39,target,组合,returnSize,int,candidates,array,now,总和 From: https://www.cnblogs.com/llllmz/p/18061409