-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapTwoSum.java
More file actions
36 lines (28 loc) · 994 Bytes
/
HashMapTwoSum.java
File metadata and controls
36 lines (28 loc) · 994 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
36
import java.util.HashMap;
import java.util.Arrays;
import java.util.Map;
public class HashMapTwoSum {
public static void main(String[] args) {
int[] nums = {2,7,11,15};
int target = 9;
int[] arr = new int[2];
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int complement = target - nums[i];
for (Integer x: map.keySet()) {
String key = x.toString();
String value = map.get(x).toString();
System.out.println(key + " " + value);
}
System.out.println("Next iteration of map: ");
System.out.println("Complement value: "+complement);
if(map.containsKey(complement)) {
arr[0] = map.get(complement);
arr[1] = i;
break;
}
map.put(nums[i], i);
}
System.out.println(Arrays.toString(arr));
}
}