代码实现:
双指针
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* twoSum(int *numbers, int numbersSize, int target, int *returnSize) {int *res = malloc(sizeof(int) * 2);*returnSize = 2;int i = 0, j = numbersSize - 1;while (i < j) {int sum = numbers[i] + numbers[j];if (sum == target) {break;}if (sum < target) {i++;if (numbers[i] == numbers[i - 1]) { // 去重i++;}}if (sum > target) {j--;if (numbers[j] == numbers[j + 1]) { // 去重j--;}}}res[0] = i + 1;res[1] = j + 1;return res; }