本帖最后由 孤不冷cing 于 2024-3-9 15:50 编辑
第一次用leetcode,第一题就无法成功运行
我在gcc中写得如下,可以按照我设想的运行#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main(void)
{
int target = 0;
int *num = calloc(MAX, sizeof(int));
int result1 = 0, result2 = 0;
int *num_d = calloc(MAX, sizeof(int));
int **num_p = calloc(MAX, sizeof(int *)); // 二元数组,脚标代表num_a中元素的值(正),内层,一个表示是否出现,一个表示其在num_a中的>脚标
for (int i = 0; i < MAX; i++)
{
num_p[i] = calloc(2, sizeof(int));
}
int **num_n = calloc(MAX, sizeof(int *)); // 二元数组,脚标代表num_a中元素的值(负),内层,一个表示是否出现,一个表示其在num_a中的>脚标
for (int i = 0; i < MAX; i++)
{
num_n[i] = calloc(2, sizeof(int));
}
scanf("nums = [");
int i = 0;
while(scanf("%d,", &num[i++]))
;
scanf("], target = %d", &target);
// calculates the num, which each element need
for (int i = 0; i < MAX; i++)
{
num_d[i] = target - num[i];
}
for (int i = 0; i < MAX; i++)
{
if (num[i] >= 0)
{
num_p[num[i]][0] = 1;
num_p[num[i]][1] = i;
}
else
{
num_n[(0 - num[i])][0] = 1;
num_n[(0 - num[i])][1] = i;
}
}
for (int i = 0; i < MAX; i++)
{
if (num_d[i] >= 0)
{
if(num_p[num_d[i]][0] == 1 && i != num_p[num_d[i]][1])
{
result1 = i;
result2 = num_p[num_d[i]][1];
break;
}
}
else
{
if(num_n[(0 - num_d[i])][0] == 1 && i != num_p[num_d[i]][1])
{
result1 = i;
result2 = num_n[(0 - num_d[i])][1];
break;
}
}
}
if((result1 != result2) && ((num[result1] + num[result2]) == target))
printf("[%d, %d]\n", result1, result2);
else
printf("no result meets the requirements.\n");
free(num);
free(num_d);
for (int i = 0; i < MAX; i++)
free(num_p[i]);
free(num_p);
for (int i = 0; i < MAX; i++)
free(num_n[i]);
free(num_n);
return 0;
}
我在leetcode中修改的如下/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
if ((returnSize = (int *)calloc(2, sizeof(int))) == NULL)
exit(EXIT_FAILURE);
int *nums_d;
if ((nums_d = (int *)calloc(numsSize, sizeof(int))) == NULL)
exit(EXIT_FAILURE);
int **nums_p;
if ((nums_p = (int **)calloc(numsSize, sizeof(int *))) == NULL)
exit(EXIT_FAILURE); // 二元数组,脚标代表num_a中元素的值(正),内层,一个表示是否出现,一个表示其在num_a中的>脚标
for (int i = 0; i < numsSize; i++)
{
if ((nums_p[i] = (int *)calloc(2, sizeof(int))) == NULL){
printf("calloc failed.\n%d\n", i);
exit(EXIT_FAILURE);
}
}
int **nums_n;
if ((nums_n = (int **)calloc(numsSize, sizeof(int *))) == NULL); // 二元数组,脚标代表num_a中元素的值(负),内层,一个表示是否出现,一个表示其在num_a中的>脚标
for (int i = 0; i < numsSize; i++)
{
if ((nums_n[i] = (int *)calloc(2, sizeof(int))) == NULL){
printf("calloc failed.\n%d\n", i);
exit(EXIT_FAILURE);
}
}
// calculates the num, which each element need
for (int i = 0; i < numsSize; i++)
{
nums_d[i] = target - nums[i];
}
for (int i = 0; i < numsSize; i++)
{
if (nums[i] >= 0)
{
nums_p[nums[i]][0] = 1;
nums_p[nums[i]][1] = i;
}
else
{
nums_n[(0 - nums[i])][0] = 1;
nums_n[(0 - nums[i])][1] = i;
}
}
for (int i = 0; i < numsSize; i++)
{
if (nums_d[i] >= 0)
{
if(nums_p[nums_d[i]][0] == 1 && i != nums_p[nums_d[i]][1])
{
returnSize[0] = i;
returnSize[1] = nums_p[nums_d[i]][1];
break;
}
}
else
{
if(nums_n[(0 - nums_d[i])][0] == 1 && i != nums_p[nums_d[i]][1])
{
returnSize[0] = i;
returnSize[1] = nums_n[(0 - nums_d[i])][1];
break;
}
}
}
free(nums_d);
for (int i = 0; i < numsSize; i++)
free(nums_p[i]);
free(nums_p);
for (int i = 0; i < numsSize; i++)
free(nums_n[i]);
free(nums_n);
return returnSize;
}
它一直在报 ERROR29 ,网上说是 calloc 这类申请内存的函数使用有误,但是鄙人技拙,看了两小时没明白是哪里或者哪些地方导致了错误,请大佬帮帮忙 |