class Solution {
public int[] twoSum(int[] nums, int target) {
int head = 0;
int tail = nums.length - 1;
while (head < tail) {
if (nums[head] + nums[tail] == target) {
return new int[]{nums[head], nums[tail]};
}
if (nums[head] + nums[tail] > target) {
tail--;
continue;
}
if (nums[head] + nums[tail] < target) {
head++;
}
}
return new int[0];
}
}
评论