1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 22:16:57 +02:00
CodeWars/6kyu/two_sum/solution.java

19 lines
506 B
Java
Raw Normal View History

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!
}
}