Leetcode题解之 —— 两数之和 II - 输入有序数组
思路
双指针
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
var twoSum = function (numbers, target) { let [left, right] = [0, numbers.length - 1];
while (right >= left) { const sum = numbers[left] + numbers[right]; if (sum === target) { return [left + 1, right + 1]; } else if (right === left) { left++; right = numbers.length - 1; } else { right--; } }
return []; };
|