-
Notifications
You must be signed in to change notification settings - Fork 0
/
S167.java
33 lines (30 loc) · 1009 Bytes
/
S167.java
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
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* LC#167 Two Sum II - Input array is sorted
* Link:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/
*/
public class S167 {
public static int[] twoSum(int[] numbers, int target) {
//two hash, time : O(n)
Map<Integer, Integer> intHash = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
intHash.put(numbers[i], i);
}
for (int i = 0; i < numbers.length; i++) {
int targetKey = target - numbers[i];
if(intHash.containsKey(targetKey)) {
int indexValue = intHash.get(targetKey);
return new int[]{i + 1, indexValue + 1};
}
}
return null;
}
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));
}
}