-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoSum.java
37 lines (26 loc) · 871 Bytes
/
TwoSum.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
34
35
36
37
import java.util.Map;
import java.util.HashMap;
class TwoSum {
public static void main(String[] args) {
int arr[]= {3,8,5,2,1};
int target=10;
int result[]=twoSum(arr,target);
System.out.println(result[0]+""+result[1]);
}
public static int[] twoSum(int[] nums, int target) {
final int[] result = new int[2];
final Map<Integer, Integer> numberToIndex = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
final int first = nums[i];
final int diff = target - first;
final Integer second = numberToIndex.get(diff);
if (second != null) {
result[0] = second;
result[1] = i;
return result;
}
numberToIndex.put(first, i);
}
return result;
}
}