- Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path2Sum.java
More file actions
Latest commit
20 lines (18 loc) · 609 Bytes
/
Copy path2Sum.java
File metadata and controls
20 lines (18 loc) · 609 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
publicclassSolution {
// DO NOT MODIFY THE LIST. IT IS READ ONLY
publicArrayList<Integer> twoSum(finalList<Integer> A, intB) {
ArrayList<Integer> ans = newArrayList<>();
Map<Integer, Integer> map = newHashMap<>();
for (inti=0; i<A.size(); i++) {
if (map.containsKey(B-A.get(i))) {
ans.add(map.get(B-A.get(i)) + 1);
ans.add(i+1);
break;
}
elseif (!map.containsKey(A.get(i))) {
map.put(A.get(i), i);
}
}
returnans;
}
}