1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-16 20:56:57 +02:00
CodeWars/6kyu/two_sum/solution.java
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

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