-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwoSum.java
46 lines (40 loc) · 1.28 KB
/
TwoSum.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// https://leetcode.com/problems/two-sum/
public class TwoSum {
private final int[] nums;
private final int target;
public TwoSum(int[] array, int target) {
this.nums = array;
this.target = target;
}
public int[] solution() {
Map<Integer, List<Integer>> numbers = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (numbers.containsKey(num)) {
numbers.get(num).add(i);
} else {
List<Integer> indices = new ArrayList<>();
indices.add(i);
numbers.put(num, indices);
}
}
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int diff = target - num;
if (numbers.containsKey(diff)) {
var indices = numbers.get(diff);
for (int index : indices) {
if (index != i) {
return new int[]{i, index};
}
}
}
}
throw new IllegalArgumentException("No two sum solution!");
}
}