I tried this problem on my pc, eclipse env, and is working good, but, on LeetCode is crashing and I don t get it why. The problem on LeetCode is:"AddressSanitizer:DEADLYSIGNAL". And i can not debug on the site, cause I do not have a premium account.
Thank you in advance.
#include "common.h"
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as returnColumnSizes array.
* Note: Both returned array and columnSizes array must be malloced, assume caller calls free().
/
int ii;
bubble_sort(nums, numsSize);
int arr = (int) calloc(numsSize 3, sizeof(int));
// for (ii = 0; ii < numsSize; ii++)
// arr[ii] = (int) calloc(3, sizeof(int));
*returnSize = 0;
int i, low, high = 0;
int sum = 0;
int target = 0;
for (i = 0; nums[i] <= 0 && i < numsSize -2;) {
low = i + 1;
high = numsSize - 1;
while ( low < high) {
sum = nums[i] + nums[low] + nums[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else {
*returnSize += 1;
arr[*returnSize-1] = (int*)malloc(sizeof(int)*3);
arr[*returnSize-1][0] = nums[i];
arr[*returnSize-1][1] = nums[low];
arr[*returnSize-1][2] = nums[high];
do high--; while(nums[high] == nums[high+1] && low < high);
}
}
do i++; while (nums[i]==nums[i-1] && i < numsSize-2);
}
return arr;}
void run_3sum() {
printf("\n ---------- Run 3Sum ----------- \n");
int n = 8;
int i = 0;
int* returnSize = (int*) malloc(sizeof(int));
int** returnColumnSizes = (int**) malloc(sizeof(int));;
int **res;
int arr[8] = {-10, 0, 10, 2, -2, 5, 3, -8};//{-2,0,1,2,-1,3};
int *nums = (int*) calloc(n+4, sizeof(int));
for (i = 0; i < n; i++) {
*nums = arr[i];
nums++;
}
nums = nums - i;
res = threeSum(nums, n, returnSize, returnColumnSizes);
printf("\n Results ---3sum ---\n");
printf_3sum(res, *returnSize, 3);