LC454. 4Sum II
作者:互联网
分析:暴力枚举复杂度为$O(n^4)$,不可行。将A,B能组成的和用map计数,查找 -C[i]-D[j] 在map中是否存在
time $O(n^2)$
space $O(n^2)$
class Solution { public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { unordered_map<int, int> abSum; for (int& a : A) for (int&b : B) ++abSum[a + b]; int res = 0; for (int& c : C) { for (int& d : D) { auto it = abSum.find(-c-d); if (it != abSum.end()) res += it->second; } } return res; } };
标签:4Sum,vector,abSum,int,res,复杂度,II,map,LC454 来源: https://www.cnblogs.com/betaa/p/12529847.html