-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0454-4sum-ii.cpp
34 lines (33 loc) · 940 Bytes
/
0454-4sum-ii.cpp
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
// Gets TLE
/*
class Solution {
public:
int fourSumCount(vector<int>& one,
vector<int>& two,
vector<int>& three,
vector<int>& four, int res = 0) {
for (auto a: one)
for (auto b: two)
for (auto c: three)
for (auto d: four)
res += (a + b + c + d == 0);
return res;
}
};
*/
class Solution {
public:
int fourSumCount(vector<int>& one,
vector<int>& two,
vector<int>& three,
vector<int>& four, int res = 0) {
unordered_map<int,int> mp;
for (auto a: one)
for (auto b: two)
mp[a + b]++;
for (auto c: three)
for (auto d: four)
if (mp.count(0 - c - d)) res += mp[0 - c - d];
return res;
}
};