Class Solution
java.lang.Object
g0101_0200.s0167_two_sum_ii_input_array_is_sorted.Solution
167 - Two Sum II - Input Array Is Sorted.<p>Easy</p>
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong> , find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> <= numbers.length</code>.</p>
<p>Return <em>the indices of the two numbers,</em> <code>index<sub>1</sub></code> <em>and</em> <code>index<sub>2</sub></code><em>, <strong>added by one</strong> as an integer array</em> <code>[index<sub>1</sub>, index<sub>2</sub>]</code> <em>of length 2.</em></p>
<p>The tests are generated such that there is <strong>exactly one solution</strong>. You <strong>may not</strong> use the same element twice.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> numbers = [2,7,11,15], target = 9</p>
<p><strong>Output:</strong> [1,2]</p>
<p><strong>Explanation:</strong> The sum of 2 and 7 is 9. Therefore, index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> numbers = [2,3,4], target = 6</p>
<p><strong>Output:</strong> [1,3]</p>
<p><strong>Explanation:</strong> The sum of 2 and 4 is 6. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 3. We return [1, 3].</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> numbers = [-1,0], target = -1</p>
<p><strong>Output:</strong> [1,2]</p>
<p><strong>Explanation:</strong> The sum of -1 and 0 is -1. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= numbers.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-1000 <= numbers[i] <= 1000</code></li>
<li><code>numbers</code> is sorted in <strong>non-decreasing order</strong>.</li>
<li><code>-1000 <= target <= 1000</code></li>
<li>The tests are generated such that there is <strong>exactly one solution</strong>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
twoSum
public int[] twoSum(int[] numbers, int target)
-