Trying Naive method using c, why only {0,0,0} wrong answer? Added std output.


  • 0
    Y
        /**
     * Return an array of arrays of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int** threeSum(int* nums, int numsSize, int* returnSize) {
    	int i, j, k, l = 0;
    	int ALLOC_SIZE = 4096;
    	int** result = (int**)calloc(ALLOC_SIZE, sizeof(int*));
    	returnSize = (int*)calloc(1, sizeof(int));
    	if (numsSize<3) return result;
    	for (i = 0; i<numsSize - 2; i++) {
    		for (j = i + 1; j<numsSize - 1; j++) {
    			for (k = j + 1; k < numsSize; k++) {
    				if (nums[i] + nums[j] + nums[k] == 0) {
    					int* sum = (int*)malloc(3 * sizeof(int));
    					sum[0] = nums[i]; sum[1] = nums[j]; sum[2] = nums[k];
    					if (l >= ALLOC_SIZE) {
    						ALLOC_SIZE *= 2;
    						result=(int**)realloc(result, ALLOC_SIZE*sizeof(int*));
    					}
    					result[l] = sum;
    					l++;
    				}
    			}
    		}
    	}
    	*returnSize = l;
    	printf("returnSize=%d\n", *returnSize);
    	if(*returnSize>0)
    	    for(i=0;i<1;i++)
    	        printf("result[%d][0]=%d,result[%d][1]=%d,result[%d][2]=%d\n",i, result[i][0],i, result[i][1],i, result[i][2]);
    	return result;
    }

  • 0
    Y

    It treated as only 1 dimension array?


Log in to reply
 

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