• 算法
    • 1.HashMap
    • 2.遍历A数组和B数组元素,计算和存到Map中,key是元素之和,value是出现次数
    • 3.遍历C数组和D数组元素,从map中查找key是-(c+d)的键值对,value值即是一个四数相加和为0的一个元组
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
    HashMap<Integer, Integer> map = new HashMap<>();
    for (int a : A) {
        for (int b : B) {
            map.put(a+b, map.getOrDefault(a+b, 0) + 1);
        }
    }

    int result = 0;
    for (int c : C) {
        for (int d : D) {
            result += map.getOrDefault(-(c+d), 0);
        }
    }
    return result;
}
func fourSumCount(A []int, B []int, C []int, D []int) int {
    dict := make(map[int]int)
    for _, a := range A {
        for _, b := range B {
            dict[a+b] += 1
        }
    }

    result := 0
    for _, c := range C {
        for _, d := range D {
            result += dict[-(c+d)]
        }
    }
    return result
}