forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0923.cpp
38 lines (37 loc) · 1.04 KB
/
0923.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
35
36
37
38
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int threeSumMulti(vector<int>& A, int target)
{
unordered_map<int, long> mem;
for (auto& a : A) mem[a]++;
long result = 0;
for (auto& it : mem)
{
for (auto& it2 : mem)
{
int i = it.first, j = it2.first, k = target - i - j;
if (!mem.count(k)) continue;
if (i == j && j == k)
result += mem[i] * (mem[i] - 1) * (mem[i] - 2) / 6;
else if (i == j && j != k)
result += mem[i] * (mem[i] - 1) / 2 * mem[k];
else if (i < j && j < k)
result += mem[i] * mem[j] * mem[k];
}
}
return result % (int)(1e9 + 7);
}
};
int main()
{
vector<int> A = {1,2,3,3,1};
int target = 5;
cout << Solution().threeSumMulti(A, target);
return 0;
}