We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个已按照 升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
index1
index2
Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
The text was updated successfully, but these errors were encountered:
/** * @param {number[]} numbers * @param {number} target * @return {number[]} */ var twoSum = function(numbers, target) { let left = 0; let right = numbers.length - 1; while(left < right) { if (numbers[left] + numbers[right] === target) { return [left + 1, right + 1]; } else if (numbers[left] + numbers[right] < target) { left++; } else { right--; } } return [-1, -1]; };
function twoSum(numbers: number[], target: number): number[] { let left = 0; let right = numbers.length - 1; while(left < right) { if (numbers[left] + numbers[right] === target) { return [left + 1, right + 1]; } else if (numbers[left] + numbers[right] < target) { left++; } else { right--; } } return [-1, -1]; };
Sorry, something went wrong.
No branches or pull requests
167. Two Sum II - Input array is sorted
给定一个已按照 升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值
index1
和index2
,其中index1
必须小于index2
。Note
Example
The text was updated successfully, but these errors were encountered: