2 sum除了这种夹逼还可以用hashmap。不过已经sorted了就夹逼吧。
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
int i = 0, j = numbers.length - 1;
while (i < j) {
if (numbers[i] + numbers[j] == target) {
res[0] = i + 1;
res[1] = j + 1;
return res;
}
if (numbers[i] + numbers[j] < target) {
i++;
} else {
j--;
}
}
return null;
}