-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.java
35 lines (27 loc) · 988 Bytes
/
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
package solutions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
// [Problem] https://leetcode.com/problems/two-sum/
class TwoSum {
// test
public static void main(String[] args) {
int[] nums = new int[]{2, 7, 11, 15};
int target = 9;
int[] expectedOutput = new int[]{0, 1};
int[] actualOutput = twoSum(nums, target);
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
}
// O(N) Hashtable solution
private static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> indexMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (indexMap.containsKey(complement)) {
return new int[]{ indexMap.get(complement), i };
}
indexMap.put(nums[i], i);
}
throw new IllegalArgumentException("No solution available");
}
}