Created
July 18, 2022 13:21
-
-
Save icameling/04de90b3cea999ddc333f782098ba023 to your computer and use it in GitHub Desktop.
#哈希表 #四数相加
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { | |
| int ret = 0; | |
| unordered_map<int, int> ab_map; | |
| for (int a : nums1) { | |
| for (int b : nums2) { | |
| ab_map[a+b]++; | |
| } | |
| } | |
| int cnt = 0; | |
| for (int c : nums3) { | |
| for (int d : nums4) { | |
| if (ab_map.find(-c-d) != ab_map.end()) | |
| cnt += ab_map[-c-d]; | |
| } | |
| } | |
| return cnt; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment