C语言双指针法
167. 两数之和 II - 输入有序数组 - 力扣(LeetCode)
/** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){ // 双指针法 int left = 0; int right = numbersSize - 1; * returnSize = 2; int* result = (int*)malloc(numbersSize*sizeof(int)); while (left < right){ if (numbers[left] + numbers[right] == target){ result[0] = left + 1; result[1] = right + 1; return result; } else if ( numbers[left] + numbers[right] >= target){ right--; } else if (numbers[left] + numbers[right] <= target){ left++; } } result[0] = -1; result[1] = -1; return result; }