Why my C code get a WA, whick works well on my pc


  • 0
    D
    int compare(const void* a,const void* b){
    	return (*(int*)a-*(int*)b);
    }
    void combinationSum2Helper(int** ret,int *returnSize,int *temp,int** columnSize,int num,int candidates[],int start,int end,int target){
    	if(target<0) return;
    	else if(target==0){
    		for(int i=0;i<num;i++) ret[*returnSize][i]=temp[i];
    		(*columnSize)[*returnSize]=num;
    		++(*returnSize);
    		return;
    	}
    	if(start<end){
    		int count=0;
    		int index;
    		for(int i=start;i<end&&candidates[i]==candidates[start];i++) count++,index=i;
    		for(int i=0;i<=count;i++){
    			for(int j=1;j<=i;j++) temp[num++]=candidates[start];
    			combinationSum2Helper(ret,returnSize,temp,columnSize,num,candidates,index+1,end,target-i*candidates[start]);
    			for(int j=1;j<=i;j++) num--;
    		}
    	}
    }
    int **combinationSum2(int candidates[],int n,int target,int **columnSize,int *returnSize){
    	qsort(candidates,n,sizeof(int),compare);
    	columnSize=(int**)malloc(sizeof(int*));
    	*columnSize=(int*)malloc(sizeof(int)*n*10);
    	returnSize=(int*)malloc(sizeof(int));
    	*returnSize=0;
    	int **ret;
    	ret=(int**)malloc(sizeof(int*)*n*10);
    	for(int i=0;i<n*10;i++) ret[i]=(int*)malloc(sizeof(int)*n);
    	int *temp=(int*)malloc(sizeof(int)*n);
    	combinationSum2Helper(ret,returnSize,temp,columnSize,0,candidates,0,n,target);
    	return ret;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.