四数相加2

Posted Alice_yufeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四数相加2相关的知识,希望对你有一定的参考价值。

class Solution 
   public int fourSumCount(int[] nums1, int[] nums2, int[] nums3,   int[] nums4) 
    int res = 0;
    HashMap<Integer, Integer> map = new HashMap<>();
    for (int i : nums1) 
        for (int j : nums2) 
            map.put(i + j, map.getOrDefault(i + j, 0) + 1);
        
    
    for (int i : nums3) 
        for (int j : nums4) 
            if (map.containsKey(-(i + j)))
                res += map.get(-(i + j));
        
    
    return res;
  

以上是关于四数相加2的主要内容,如果未能解决你的问题,请参考以下文章

代码随想录算法训练营第七天 | 454.四数相加II ,383. 赎金信 ,15. 三数之和,18. 四数之和

(哈希表)Java 求解四数相加 II

leetcode 454. 四数相加 II java

454哈希表-四数相加

日常刷题LeetCode——四数相加 II

日常刷题LeetCode——四数相加 II