代码如下: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[0]=i+1;returnSize[1]=j+1;return returnSize;}运行的时候出不来结果啊,请问这是为什么,我该怎么改呢?
1 回答
波斯汪
TA贡献1811条经验 获得超4个赞
不考虑你程序本身的对错,仅仅从输入输出的角度来说,你返回的是结果数组的指针,而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; }
应该是这么一个逻辑
- 1 回答
- 0 关注
- 2302 浏览
添加回答
举报
0/150
提交
取消