|
| 1 | +<h2><a href="https://leetcode.com/problems/two-sum-ii-input-array-is-sorted">167. Two Sum II - Input Array Is Sorteda>h2><h3>Mediumh3><hr><p>Given a <strong>1-indexedstrong> array of integers <code>numberscode> that is already <strong><em>sorted in non-decreasing orderem>strong>, find two numbers such that they add up to a specific <code>targetcode> number. Let these two numbers be <code>numbers[index<sub>1sub>]code> and <code>numbers[index<sub>2sub>]code> where <code>1 <= index<sub>1sub> < index<sub>2sub> <= numbers.lengthcode>.p> |
| 2 | + |
| 3 | +<p>Return<em> the indices of the two numbers, em><code>index<sub>1sub>code><em> and em><code>index<sub>2sub>code><em>, <strong>added by onestrong> as an integer array em><code>[index<sub>1sub>, index<sub>2sub>]code><em> of length 2.em>p> |
| 4 | + |
| 5 | +<p>The tests are generated such that there is <strong>exactly one solutionstrong>. You <strong>may notstrong> use the same element twice.p> |
| 6 | + |
| 7 | +<p>Your solution must use only constant extra space.p> |
| 8 | + |
| 9 | +<p> p> |
| 10 | +<p><strong class="example">Example 1:strong>p> |
| 11 | + |
| 12 | +<pre> |
| 13 | +<strong>Input:strong> numbers = [<u>2u>,<u>7u>,11,15], target = 9 |
| 14 | +<strong>Output:strong> [1,2] |
| 15 | +<strong>Explanation:strong> The sum of 2 and 7 is 9. Therefore, index<sub>1sub> = 1, index<sub>2sub> = 2. We return [1, 2]. |
| 16 | +pre> |
| 17 | + |
| 18 | +<p><strong class="example">Example 2:strong>p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input:strong> numbers = [<u>2u>,3,<u>4u>], target = 6 |
| 22 | +<strong>Output:strong> [1,3] |
| 23 | +<strong>Explanation:strong> The sum of 2 and 4 is 6. Therefore index<sub>1sub> = 1, index<sub>2sub> = 3. We return [1, 3]. |
| 24 | +pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 3:strong>p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:strong> numbers = [<u>-1u>,<u>0u>], target = -1 |
| 30 | +<strong>Output:strong> [1,2] |
| 31 | +<strong>Explanation:strong> The sum of -1 and 0 is -1. Therefore index<sub>1sub> = 1, index<sub>2sub> = 2. We return [1, 2]. |
| 32 | +pre> |
| 33 | + |
| 34 | +<p> p> |
| 35 | +<p><strong>Constraints:strong>p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + 2 <= numbers.length <= 3 * 104 |
| 39 | + -1000 <= numbers[i] <= 1000 |
| 40 | + numbers is sorted in non-decreasing order. |
| 41 | + -1000 <= target <= 1000 |
| 42 | + The tests are generated such that there is exactly one solution. |
| 43 | +ul> |
0 commit comments