马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/*给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/
#include <stdio.h>
#include <stdlib.h>
int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
int i = 0, j = 0, flag = 0;
returnSize = (int *)malloc(sizeof(int) * 2);
for (int x = 0; x < numsSize; x++)
{
int t = 0;
for (int y = 0; y < x; y++)
{
if (nums[x] < nums[y])
{
t = nums[x];
nums[x] = nums[y];
nums[y] = t;
}
}
}
if ((target < nums[0] + nums[1]) || (target > nums[numsSize - 2] + nums[numsSize - 1]))
{
printf("超出范围\n");
returnSize[0] = NULL;
returnSize[1] = NULL;
}
else
{
for (i = 0, j = 0; i < numsSize; i++)
{
for (j = numsSize; j >= 0; j--)
{
if (nums[i] + nums[j] == target)
{
flag++;
if (flag > 0)
{
returnSize[0] = i;
returnSize[1] = j;
break;
}
}
}
if (flag > 0)
{
break;
}
}
if (flag < 1)
{
printf("NO NUMBER\n");
returnSize[0] = 0;
returnSize[1] = 0;
}
}
return returnSize;
}
int main(int argc, char const *argv[])
{
int arr[] = {2, 7, 11, 15};
int s = sizeof(arr) / sizeof(arr[0]);
int key = 10;
int *result;
result = twoSum(arr, s, key, result);
printf("(%d , %d)", result[0], result[1]);
return 0;
}
这段代码我在VScode运行正常
在Leecode在线编译器上一堆错误
solution.c: In function 'twoSum'
Line 23: Char 17: warning: assignment to 'int' from 'void *' makes integer from pointer without a cast [-Wint-conversion]
returnSize[0] = NULL;
^
Line 24: Char 17: warning: assignment to 'int' from 'void *' makes integer from pointer without a cast [-Wint-conversion]
returnSize[1] = NULL;
^
solution.c: At top level
Line 74: Char 5: error: conflicting types for 'main'
到底是咋回事
本帖最后由 Croper 于 2019-7-22 12:16 编辑
你别把main()给一起粘贴上去啊,
leetcode只要你给出要求的函数,别画蛇添足
|