1. 程式人生 > 實用技巧 >【leetcode】40. 組合總和 II

【leetcode】40. 組合總和 II

int cmp(const void* a,const void* b){
    return *(int*)a-*(int*)b;
}
void recursion(int* candidates,int candidatesSize,int** arr,int* temp,int pst,int target,int res,int* returnSize,int* returnColumnSizes,int start){    
    for(int i=start; i<candidatesSize && 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,candidatesSize,arr,temp,pst+1,target,res+candidates[i],returnSize,returnColumnSizes, i+1); while(i!=candidatesSize-1 && candidates[i]==candidates[i+1]) i++; temp[pst]
=0; } } int** combinationSum2(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; }