参考答案:
public static int[] twoSum(int[] numbers, int target) { int i = 0, j = numbers.length - 1; while (i != j) { if (numbers[i] + numbers[j] == target) { return new int[]{i + 1, j + 1}; } if (numbers[i] + numbers[j] < target) { i++; continue; } if (numbers[i] + numbers[j] > target) { j--; continue; } } return new int[]{i, j}; }
分析:空间复杂度和时间复杂度均为 O(n)