how to set the para *columnSizes*


  • 0
    W

    how to set the para columnSizes

    this is the c function to solve this problem,

    int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
    

    and this is some help info about para

    /**
     * Return an array of arrays of size *returnSize.
     * The sizes of the arrays are returned as *columnSizes array.
     * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
     */
    

    this is my solution

    void swap(int a[], int i, int j){
    	int temp=a[i];
    	a[i]=a[j];
    	a[j]=temp;
    }
    
    int pivot(int a[], int p, int r){
    	int x=a[r];
    	int i=p;
    	int j=i-1;
    	for(;i<r;i++){
    		if(a[i]<x){
    			j++;
    			swap(a,i,j);
    		}
    	}
    	swap(a,j+1,r);
    	return j+1;
    }
    
    void quick_sort(int a[], int p, int r){
    	if(p<r){
    		int q=pivot(a,p,r);
    		quick_sort(a,p,q-1);
    		quick_sort(a,q+1,r);
    	}
    }
    
    void get_can(int target,int *candidates, int candidatesSize,int ** ret,int *num, int stack[], int depth,int* temp,int **columnSizes){
    	if(target==0){
    
    		quick_sort(temp,0,depth-1);
    
    		if(!(*num)){
    			for(int j=0;j<depth;j++){
    				ret[*num][j]=temp[j];
    			}
    
    			columnSizes[*num][0]=depth;
    			(*num)++;
    		}
    		else{
    
    			int had=0;
    			for(int i=0;i<(*num);i++){
    				int h=1;
    				for(int j=0;j<depth;j++){
    					if(ret[i][j]!=temp[j]){
    						h=0;
    						break;
    					}
    				}
    				if(h==1){
    					had=1;
    					break;
    				}
    			}
    			
    			
    			if(!had){
    				for(int j=0;j<depth;j++){
    					ret[*num][j]=temp[j];
    				}
    			    columnSizes[*num][0]=depth;
    				(*num)++;
    			}
    		}
    		
    		for(int j=0;j<depth;j++){
    			temp[j]=stack[j];
    		}
    		return;
    	}
    	else if(target>0){
    		for(int j=0;j<candidatesSize;j++){
    			if(target-candidates[j]>=0){
    				stack[depth]=candidates[j];
    				temp[depth]=candidates[j];
    				get_can(target-candidates[j],candidates,candidatesSize ,ret,num,stack, depth+1,temp,columnSizes);
    			}
    			else{
    				return;
    			}
    		}
    	}
    	else{
    		return;
    	}
    
    }
    
    int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
    	quick_sort(candidates,0,candidatesSize-1);
    
    	int n=1000;
    
    	//returnSize=(int *)calloc(n,sizeof(int));
    
    	int ** ret=(int **)malloc(n*sizeof(int *));
        columnSizes=(int **)malloc(n*sizeof(int *));
    
    	int e_size=target/candidates[0]+1;
    
    	for(int i=0;i<n;i++){
    		ret[i]=(int *)calloc(e_size,sizeof(int));
            columnSizes[i]=(int *)calloc(1,sizeof(int));
    	}
    
    	int num=0;
    	int* stack=(int *)calloc(e_size,sizeof(int));
    	int* temp=(int *)calloc(e_size,sizeof(int));
    	int depth=1;
    
    	for(int i=0;i<candidatesSize;i++){
    		if(target-candidates[i]>=0){
    			stack[0]=candidates[i];
    			temp[0]=candidates[i];
    			get_can(target-candidates[i],candidates,candidatesSize, ret,&num,stack, depth,temp,columnSizes);
    		}
    		else{
    			break;
    		}
    	}
    	*returnSize=num;
    	return ret;
    
    }
    

    I don't know how to set the para columnSizes, who can help me?


Log in to reply
 

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