-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowSum.java
More file actions
27 lines (23 loc) · 749 Bytes
/
Copy pathTowSum.java
File metadata and controls
27 lines (23 loc) · 749 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
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
public class TowSum {
public static void main(String[] args) {
int[] nums = new int[] {3,3};
int[] select = twoSUm( nums, 6);
}
public static int[] twoSUm ( int[] nums, int target) {
int[] result = new int[2];
Map<Integer,Integer> map = new HashMap<Integer,Integer>() ;
for(int i =0 ; i< nums.length ; i++) {
int value = target - nums[i];
if(map.containsKey(value)) {
result[0] = i;
result[1] = map.get(value);
break;
}
map.put(nums[i],i);
}
return result;
}
}