class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0, right = numbers.length - 1;
while (left <= right) {
int sum = numbers[left] + numbers[right];
if (sum == target) {
// 题目规定下标从 1 开始计数
return new int[]{left + 1, right + 1};
} else if (sum < target) {
// 增大 sum 值
left++;
} else if (sum > target) {
// 减小 sum 值
right--;
}
}
return new int[]{-1, -1};
}
}
leetcode-167-两数之和 II - 输入有序数组
最新推荐文章于 2024-08-23 01:05:03 发布