454. 四数相加 II
思路:先将 A+B 的和储存在 map 中 (a+b, times),然后计算 C+D。
代码:
class Solution { public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { unordered_map<int, int> hash_AB; int a, b, c, d, count = 0, sum; for(const auto &a:A) { for(const auto&b:B) { hash_AB[a+b]++; } } for(const auto &c:C) { for(const auto &d:D) { sum = -(c+d); if(hash_AB[sum]!=0) { count += hash_AB[sum]; } } } return count; } };
原文:https://www.cnblogs.com/jessica216/p/13507599.html