两数之和
import java.util.*;
public class TwoSum {
public static List<List<Integer>> twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
List<List<Integer>> resultList = new ArrayList<>();
for (int i = 1; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int d = target - nums[i];
if (map.containsKey(d) && map.get(d) != i) {
resultList.add(Arrays.asList(nums[i], d));
//为防止找到重复的元素对,匹配后从哈希表删除对应元素
map.remove(nums[i]);
}
}
return resultList;
}
public static List<List<Integer>> twoSumV2(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
List<List<Integer>&g