forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchjung99.java
More file actions
25 lines (22 loc) · 828 Bytes
/
chjung99.java
File metadata and controls
25 lines (22 loc) · 828 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
import java.util.*;
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, List<Integer>> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (!map.containsKey(nums[i])) {
map.put(nums[i], new ArrayList<Integer>());
}
map.get(nums[i]).add(i);
}
for (Map.Entry<Integer, List<Integer>> e: map.entrySet()) {
int other = target - e.getKey();
if (map.containsKey(other)) {
if (e.getKey() == other && map.get(other).size() > 1) {
return new int[]{e.getValue().get(0), e.getValue().get(1)};
}
return new int[]{e.getValue().get(0), map.get(other).get(0)};
}
}
return new int[]{};
}
}