leetcode 39组合总和 c语言实现(回溯法 宽度优先)

int cmpfunc (const void * a, const void * b){return ( *(int*)a - *(int*)b );}
int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
    if(candidatesSize<=0)return NULL;
    qsort(candidates,candidatesSize,sizeof(int),cmpfunc);
    int size=target/candidates[0],len=500;//最有可能大的列宽,满n叉树的叶子数
    int **a=(int **)calloc(len,sizeof(int *)),**b=(int **)calloc(len,sizeof(int *)),**tmp=NULL;
    int **res=(int **)calloc(len,sizeof(int *));
    *columnSizes=(int *)calloc(len,sizeof(int));
    for(int j=0;jtarget)break;                
                if((a[k][size+1]+candidates[l])==target){
                   (*columnSizes)[(*returnSize)]=temp+1;
                    res[(*returnSize)]=(int*)calloc(temp+1,sizeof(int));
                    memcpy(res[(*returnSize)],a[k],sizeof(int)*(temp));
                    res[(*returnSize)++][temp]=candidates[l];
                }
                else {
                    if(target-(a[k][size+1]+candidates[l])

 

你可能感兴趣的:(leetcode 39组合总和 c语言实现(回溯法 宽度优先))