1. Two Sum 发表于 2021-12-05 1234567891011121314class 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"); }} Reference1. Two Sum