Skip to content

Instantly share code, notes, and snippets.

@icameling
Created July 18, 2022 13:21
Show Gist options
  • Select an option

  • Save icameling/04de90b3cea999ddc333f782098ba023 to your computer and use it in GitHub Desktop.

Select an option

Save icameling/04de90b3cea999ddc333f782098ba023 to your computer and use it in GitHub Desktop.
#哈希表 #四数相加
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