四数之和,看成两数之和与另外两数之和,然后使用哈希。
class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer,Integer> AB=new HashMap<Integer, Integer>();
for(int i=0;i<A.length;i++){
for(int j=0;j<B.length;j++){
int temp1=A[i]+B[j];
AB.put(temp1,AB.getOrDefault(temp1,0)+1);
}
}
int res=0;
for(int i=0;i<C.length;i++)
for(int j=0;j<D.length;j++)
res+=AB.getOrDefault(-(C[i]+D[j]),0);
return res;
}
}