1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> valueToIndex = new HashMap<>();
for (int i = 0; i < nums.length; i++) { int preNum = target - nums[i]; if (valueToIndex.containsKey(preNum)) { return new int[] {valueToIndex.get(preNum), i}; } valueToIndex.put(nums[i], i); }
throw new RuntimeException(); } }
|