要求:四个数组的数相加为0
思路:不能用指针了,用两个哈希表
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> countAB;
for (int u: A) {
for (int v: B) {
++countAB[u + v];
}
}
int ans = 0;
for (int u: C) {
for (int v: D) {
if (countAB.count(-u - v)) {
ans += countAB[-u - v];
}
}
}
return ans;
}
};