Skip to main content

167. Two Sum II - Input Array Is Sorted

分析


题解

function twoSum(numbers: number[], target: number): number[] {
let l = 0, r = numbers.length - 1
while (l < r) {
if (numbers[l] + numbers[r] < target) {
l = l + 1
} else if (numbers[l] + numbers[r] > target) {
r = r - 1
} else {
return [l + 1, r + 1]
}
}
};