原题链接 首先定义一个hashmap 用来储存结果一共有四个数组,分为两组。先遍历计算前两组的两数之和temp,当map中出现了重复的temp的时候,将对应value+1,反之则执行将键为temp的值赋值为1之后计算另外的两组遍历计算两数之和,当在map中出现了,此时两数之和的相反数的时候,即说明此时的两数之和,与之前的计算和可以满足相加为0的条件这时,将res进行 res += map.get(0-temp)二刷:在前半部分的往map中存值得过程中,当出现重复的时候,要将重复值得value + 1,具体操作是 map.put( temp, map.get(temp) + 1); 在后半部分的计算时,要需要同一个temp来计算后面两个数组中值相加的结果,只需使用res来记录:有相反数的temp 所对应的value即可三刷:当调用方法的时候,一定时某个对象来调用这个方法,比如map.get(temp); class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { Map<Integer, Integer> map = new HashMap<>(); int temp = 0; int res = 0; for(int i : nums1){ for(int j : nums2){ temp = i + j; if(map.containsKey(temp)){ map.put(temp, map.get(temp) + 1); }else{ map.put(temp, 1); } } } for(int i : nums3){ for(int j : nums4){ temp = i + j; if(map.containsKey(0 - temp)){ res += map.get(0 - temp); } } } return res; } }