一、题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/97947099b4e26d25d304071bdc3a3dad.png)
二、代码
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
int nums_size=nums1.size();
std::cout<<"nums_size "<<nums_size<<std::endl;
int i,j;
int temp_sum=0;
int now_num=0;
int target_num=0;
int return_count=0;
map<int,int> sum_of_12;
map<int,int> sum_of_34;
map<int, int>::iterator it;
map<int, int>::iterator target_it;
for(i=0;i<nums_size;i++)
{
for(j=0;j<nums_size;j++)
{
temp_sum=nums1[i]+nums2[j];
it=sum_of_12.find(temp_sum);
if(it!=sum_of_12.end())
{
it->second=it->second+1;
}
else
{
sum_of_12.insert(pair<int, int>(temp_sum, 1));
}
}
}
for(i=0;i<nums_size;i++)
{
for(j=0;j<nums_size;j++)
{
temp_sum=nums3[i]+nums4[j];
it=sum_of_34.find(temp_sum);
if(it!=sum_of_34.end())
{
it->second=it->second+1;
}
else
{
sum_of_34.insert(pair<int, int>(temp_sum, 1));
}
}
}
for (map<int, int>::iterator it = sum_of_12.begin(); it != sum_of_12.end(); it++)
{
now_num=it->first;
target_num=0-now_num;
target_it=sum_of_34.find(target_num);
if(target_it!=sum_of_34.end())
{
return_count=return_count+target_it->second*it->second;
}
}
return return_count;
}
};
三、运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5a335d59d6f38928650178e6ece3efc2.png)