public class Solution {
public int FourSumCount(int[] A, int[] B, int[] C, int[] D) {
var dic = new Dictionary<int, int>(); for (int i = ; i < C.Length; i++)
{
for (int j = ; j < D.Length; j++)
{
int sum = C[i] + D[j];
if (!dic.ContainsKey(sum))
{
dic.Add(sum, );
}
else
{
dic[sum]++;
}
}
} int res = ;
for (int i = ; i < A.Length; i++)
{
for (int j = ; j < B.Length; j++)
{ var cur = ;
var oppo = - * (A[i] + B[j]);
if (dic.ContainsKey(oppo))
{
cur = dic[oppo];
}
res += cur;
}
} return res;
}
}
https://leetcode.com/problems/4sum-ii/#/description
补充一个python的版本:
class Solution:
def fourSumCount(self, A: 'List[int]', B: 'List[int]', C: 'List[int]', D: 'List[int]') -> int:
partone = {}
res =
for a in A:
for b in B:
cur = a + b
if cur in partone:
partone[cur] +=
else:
partone[cur] = for c in C:
for d in D:
cur = c + d
if -cur in partone:
res += partone[-cur] return res