1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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); } } return new int[0]; } }
|