Could someone help me find out why it is RE?


  • 0
    F

    Could someone help me find out why it is RE. I had run it offline and it's correct, but it return RE after I submit it.

    //using backtrack to find out the solutions of combinations C(n,k) from array data
    int combine(int* data, int n, int k,int** ret,int size, int old_count){
    	int count = old_count;
    	
    	if( n < k || k == 0 ){
    	  return 0;  
    	} 
    	if( k == 1 ){
    		for( int i = 0; i < n; ++ i){
    			ret[count] = (int*)malloc(sizeof(int)*size);
    			ret[count++][0] = data[i];
    		}
    		return n;
    	}
    	int i,j;
    	
    	for( i = n - 1; i >= k - 1; -- i ){
    		int cnt = combine(data, i, k - 1, ret, size, count);
    		for( j = 0; j < cnt; ++ j ) ret[count+j][k-1] = data[i];
    		count += cnt;
    	}
    	
    	return count - old_count;
    	
    }
    int cmp(const void* a, const void *b){
    	return *(int*)a - *(int*)b;
    }
    
    
    int** subsets(int* nums, int numsSize, int** columnSizes, int* returnSize) {
    	int count = 1<<numsSize;
    	*returnSize = count;
    	int** ret = (int**)malloc(sizeof(int*)*count);
    	*columnSizes = (int*)malloc(sizeof(int)*count);
    	qsort(nums,numsSize,sizeof(int),cmp);
    	count = 0;
     
    	//visit all size of combinations
    	for( int k = 1; k <= numsSize; ++ k ){
    		int cnt = combine(nums,numsSize,k,ret,k,count);
    		for( int i = count; i < count + cnt; ++ i ){
    			(*columnSizes)[i] = k;
    		}
    		count += cnt;
    	}
    	
    	(*columnSizes)[ (*returnSize) - 1 ] = 0;
    	return ret;
    }

Log in to reply
 

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