hash
求前2个数组的所有可能和和后2个数组的所有可能和,使问题退化为一个two sum
问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { unordered_map<int, int> map; for(auto n1 : nums1) for(auto n2 : nums2) ++map[n1 + n2]; int ret = 0; for(auto n3 : nums3) for(auto n4 : nums4) ret += map[-(n3 + n4)]; return ret; } };
|