lurenaa的博客

😂失败1.超出时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
set<vector<int>> st;
for(int a = 0; a < A.size(); ++a)
for(int b = 0; b <B.size(); ++b)
for(int c = 0; c < C.size(); ++c)
for(int d = 0; d < D.size(); ++d)
{
if(A[a] + B[b] + C[c] + D[d] == 0) {
st.insert({a,b,c,d});
}
}
return st.size();
}
};

😜分为两组

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
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
map<int,int> st;
int count = 0;
int x;
for(int a = 0; a < A.size(); ++a)
for(int b = 0; b <B.size(); ++b)
{
x = A[a] + B[b];
if(!st.count(x))
st.insert({x, 1});
else
st[x] += 1;
}

for(int c = 0; c < C.size(); ++c)
for(int d = 0; d < D.size(); ++d)
{
x = C[c] + D[d];
if(st.count(-x)) {
count += st[-x];
}
}
return count;
}
};