-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0454_fourSumCount.cc
68 lines (62 loc) · 1.24 KB
/
Problem_0454_fourSumCount.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int fourSumCount(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3, vector<int> &nums4)
{
unordered_map<int, int> map;
int sum = 0;
for (int i = 0; i < nums1.size(); i++)
{
for (int j = 0; j < nums2.size(); j++)
{
sum = nums1[i] + nums2[j];
if (!map.count(sum))
{
map.emplace(sum, 1);
}
else
{
map[sum]++;
}
}
}
int ans = 0;
for (int i = 0; i < nums3.size(); i++)
{
for (int j = 0; j < nums4.size(); j++)
{
sum = nums3[i] + nums4[j];
if (map.count(-sum))
{
ans += map.at(-sum);
}
}
}
return ans;
}
};
void testFourSumCount()
{
Solution s;
vector<int> n1 = {1, 2};
vector<int> n2 = {-2, -1};
vector<int> n3 = {-1, 2};
vector<int> n4 = {0, 2};
vector<int> m1 = {0};
vector<int> m2 = {0};
vector<int> m3 = {0};
vector<int> m4 = {0};
EXPECT_EQ_INT(2, s.fourSumCount(n1, n2, n3, n4));
EXPECT_EQ_INT(1, s.fourSumCount(m1, m2, m3, m4));
EXPECT_SUMMARY;
}
int main()
{
testFourSumCount();
return 0;
}