← 返回 oracle 的题目列表Leetcode Two Pointers Original Question
类型:online_judge
Given an array of integers numbers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function should return the indices of the two numbers index1 and index2, where 1 <= index1 < index2 <= numbers.length.
Notice:
Your solution must use only constant extra space and has a linear runtime complexity O(n).
You may not use the same element twice.
Assume each input will have exactly one solution.
Input format:
The first line contains two integers n and target, the length of the array and the target sum.
The second line contains n integers sorted in ascending order representing the array.
Output format:
Output two integers, representing the indices of the two numbers (1-based index) in ascending order.
Example:
Input
5 9
1 2 3 4 5
Output
3 4
Constraints:
2 <= n <= 30000
-1000 <= numbers[i] <= 1000
-1000 <= target <= 1000
numbers is sorted in ascending order.
Example
Input
5 9
1 2 3 4 5