-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum_2.java
31 lines (29 loc) · 900 Bytes
/
TwoSum_2.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
import java.util.HashMap;
public class TwoSum_2 {
public int[] twoSum(int[] numbers, int target) {
int[] ans = new int[2];
HashMap<String,Integer> hashMap = new HashMap<>();
for(int i =0;i < numbers.length;i++){
if( hashMap.containsKey(""+numbers[i]) ){
Integer a = hashMap.get(""+numbers[i])+1;
ans[0] = a;
ans[1] = i+1;
}
else{
int complement = target-numbers[i];
hashMap.put(""+complement+"", i);
}
}
return ans;
}
//Prototype Main
public static void main(String[] args) {
int nums[] = {-1,0};
int target = -1;
TwoSum_2 twosum= new TwoSum_2();
int ans[] = twosum.twoSum(nums, target);
for (int i : ans) {
ConsoleLogger.writeInfo(i);
}
}
}