【leetcode】39. 组合总和

 

int cmp(const void* a,const void* b){
    return *(int*)a-*(int*)b;
}
void recursion(int* candidates,int len,int** arr, int* temp, int pst, int target, int res,int* returnSize,int* returnColumnSizes,int start){    
    for(int i=start; i<len && res<target; i++){
        temp[pst]=candidates[i];
        if(res+candidates[i] >= target){
            if(res+candidates[i]==target){
                arr[(*returnSize)]=(int*)calloc(pst+1,sizeof(int));
                memcpy(arr[(*returnSize)],temp,++pst*sizeof(int));
                returnColumnSizes[(*returnSize)]=pst;
                (*returnSize)++;
            }
            break;        
        }            
        recursion(candidates,len,arr,temp,pst+1,target,res+candidates[i],returnSize,returnColumnSizes, i);
        temp[pst]=0;
    }
}

int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes){
    int** arr=(int**)calloc(1000,sizeof(int*));
    *returnColumnSizes=(int*)calloc(1000,sizeof(int));
    *returnSize=0;
    int temp[1000]={0};
    qsort(candidates,candidatesSize,sizeof(int),cmp);
    recursion(candidates,candidatesSize,arr,temp,0,target,0,returnSize,*returnColumnSizes,0);
    return arr;
}

 

上一篇:从上到下打印二叉树,并且用数组从左到右存储


下一篇:lvm逻辑卷的创建、扩展与删除