import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
@Test
public void test() {
int[] array = new int[]{2, 7, 11, 15};
int target = 9;
int[] result = new int[]{0, 1};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));
array = new int[]{3, 2, 4};
target = 6;
result = new int[]{1, 2};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));
array = new int[]{3, 3};
target = 6;
result = new int[]{0, 1};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));
}
public int[] twoSum(int[] array, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
return new int[]{map.get(array[i]), i};
}
map.put(target - array[i], i);
}
return null;
}
}