第二个for循环数组越界了,b应该最大为numssize-1
如有帮助,请点击我评论上方【采纳该答案】按钮支持一下,谢谢!以后有什么问题可以互相交流。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
for (int a = 0; a < numsSize; ++a) {
for (int b = numsSize-1; b >a; --b) {
if (nums[a] + nums[b] == target) {
int* ret = malloc(sizeof(int) * 2);
ret[0] = a, ret[1] = b;
*returnSize = 2;
return ret;
}
}
}
*returnSize = 0;
return NULL;
}