-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathD3TwoSum.java
More file actions
30 lines (27 loc) · 1.01 KB
/
D3TwoSum.java
File metadata and controls
30 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.haobin.leetcode21;
/**
*
* 给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 <= index1 < index2 <= numbers.length 。
*
* 以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 和 index2
*
*
* @Date 2022/6/13 9:42 AM
* @author: leobhao
*/
public class D3TwoSum {
public int[] twoSum(int[] numbers, int target) {
int leftIndex = 0, rightIndex = numbers.length - 1;
while (leftIndex <= rightIndex) {
int sum = numbers[leftIndex] + numbers[rightIndex];
if (sum == target) {
return new int[] {leftIndex+1, rightIndex+1};
} else if (sum < target) {
leftIndex++;
} else {
rightIndex--;
}
}
return new int[] {-1, -1};
}
}