波斯汪
不考虑你程序本身的对错,仅仅从输入输出的角度来说,你返回的是结果数组的指针,而returnsize的作用是返回,告知调用者结果数组的大小 /* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* numbers, int numbersSize, int target, int * returnSize) {
int i=0;
int j=numbersSize-1;
while(numbers[i]+numbers[j]!=target)
{
if(numbers[i]+numbers[j]>target)
{
j=j-1;
}
else
{
i=i+1;
}
}
*returnSize = 2;
int * r = new int[2];
r[0]=i+1;
r[1]=j+1;
return r;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[] = {2,7,15,9};
int t = 9;
int rs;
int * r = twoSum(a, 4, t, &rs);
for (int i = 0; i < rs; i++)
printf("%d ", r[i]);
return 0;
}应该是这么一个逻辑