-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSum2MutilSolution.java
32 lines (30 loc) · 1.06 KB
/
Sum2MutilSolution.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
import java.util.*;
public class Sum2MutilSolution {
public static ArrayList<ArrayList<Integer>> twoSum(int[] numbers, int target) {
//int i = 0;
int len = numbers.length;
HashMap<Integer,Integer> mapHelper = new HashMap<Integer,Integer>();
// ArrayList<Integer> L1= new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> L2 = new ArrayList<ArrayList<Integer>>();
for(int i = 0 ;i < len ;i++){
if(mapHelper.containsKey(numbers[i])){
int realPos1 = mapHelper.get(numbers[i])+1;
int realPos2 = i+1;
ArrayList<Integer> L1= new ArrayList<Integer>();
L1.add(realPos1);
L1.add(realPos2);
L2.add(L1);
}
else{
mapHelper.put(target-numbers[i],i);
}
}
return L2;
}
public static void main(String args[]){
int[] A = {1, 2, 8, 9};
int target = 10;
//Integer a = new Integer(1);
System.out.println(twoSum(A,target));
}
}