-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
31 lines (26 loc) · 824 Bytes
/
TwoSum.java
File metadata and controls
31 lines (26 loc) · 824 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
/*
int[] nums = {2, 7, 11, 15};
int target = 9;
Output should be the indices [0, 1] because nums[0] + nums[1] = 2 + 7 = 9.
*/
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
public static int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < nums.length;i++){
int value = target - nums[i];
if(map.containsKey(value)){
return new int[] {map.get(value),i};
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
int[] nums = {2, 5, 15, 7};
int target = 9;
int [] result = twoSum(nums,target);
System.out.println(result[0] + "," +result[1]);
}
}