Poison

1. Two Sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> valueToIndexMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
Integer anotherIndex = valueToIndexMap.get(target - nums[i]);
if (anotherIndex != null) {
return new int[]{i, anotherIndex};
} else {
valueToIndexMap.put(nums[i], i);
}
}
throw new RuntimeException("Cannot find target sum in nums");
}
}
Reference

1. Two Sum