mirror of
https://gitlab.com/mfocko/CodeWars.git
synced 2024-11-08 18:49:07 +01:00
18 lines
506 B
Java
18 lines
506 B
Java
import java.util.Map;
|
|
import java.util.HashMap;
|
|
|
|
public class Solution {
|
|
public static int[] twoSum(int[] numbers, int target) {
|
|
Map<Integer, Integer> encountered = new HashMap<>();
|
|
|
|
for (int i = 0; i < numbers.length; i++) {
|
|
Integer other = encountered.get(target - numbers[i]);
|
|
if (other != null) {
|
|
return new int[] {other, i};
|
|
}
|
|
encountered.put(numbers[i], i);
|
|
}
|
|
|
|
return null; // Do your magic!
|
|
}
|
|
}
|