-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSumII.java
More file actions
35 lines (26 loc) · 766 Bytes
/
TwoSumII.java
File metadata and controls
35 lines (26 loc) · 766 Bytes
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
31
32
33
34
35
import java.util.Arrays;
public class TwoSumII {
public static int[] twoSum(int[] numbers, int target) {
int left = 0;
int right = numbers.length - 1;
while (left < right) {
int sum = numbers[left] + numbers[right];
if (sum == target) {
return new int[]{left + 1, right + 1};
}
else if (sum < target) {
left++;
}
else {
right--;
}
}
return new int[]{-1, -1};
}
public static void main(String[] args) {
int[] numbers = {2,7,11,15};
int target = 9;
int[] result = twoSum(numbers, target);
System.out.println(Arrays.toString(result));
}
}