167. Two Sum II - Input Array Is Sorted Medium
1/**
2 * [167] Two Sum II - Input Array Is Sorted
3 *
4 * Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
5 * Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
6 * The tests are generated such that there is exactly one solution. You may not use the same element twice.
7 * Your solution must use only constant extra space.
8 *
9 * Example 1:
10 *
11 * Input: numbers = [<u>2</u>,<u>7</u>,11,15], target = 9
12 * Output: [1,2]
13 * Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
14 *
15 * Example 2:
16 *
17 * Input: numbers = [<u>2</u>,3,<u>4</u>], target = 6
18 * Output: [1,3]
19 * Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
20 *
21 * Example 3:
22 *
23 * Input: numbers = [<u>-1</u>,<u>0</u>], target = -1
24 * Output: [1,2]
25 * Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
26 *
27 *
28 * Constraints:
29 *
30 * 2 <= numbers.length <= 3 * 10^4
31 * -1000 <= numbers[i] <= 1000
32 * numbers is sorted in non-decreasing order.
33 * -1000 <= target <= 1000
34 * The tests are generated such that there is exactly one solution.
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
40// discuss: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
46 vec![]
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_167() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.