Skip to content

Commit b20aa66

Browse files
authored
feat: add cpp solution to lc problem: No.0923 (#788)
No.0923.3Sum With Multiplicity
1 parent 95607cc commit b20aa66

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

solution/0900-0999/0923.3Sum With Multiplicity/README.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,29 @@ arr[i] = 1, arr[j] = arr[k] = 2 出现 12 次:
6868

6969
```
7070

71-
### **...**
72-
73-
```
74-
71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
int threeSumMulti(vector<int>& arr, int target) {
77+
unordered_map<int, long> c;
78+
for (int a : arr) c[a]++;
79+
long res = 0;
80+
for (auto it : c)
81+
for (auto it2 : c) {
82+
int i = it.first, j = it2.first, k = target - i - j;
83+
if (!c.count(k)) continue;
84+
if (i == j && j == k)
85+
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
86+
else if (i == j && j != k)
87+
res += c[i] * (c[i] - 1) / 2 * c[k];
88+
else if (i < j && j < k)
89+
res += c[i] * c[j] * c[k];
90+
}
91+
return res % int(1e9 + 7);
92+
}
93+
};
7594
```
7695
7796
<!-- tabs:end -->

solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,29 @@ and two 2s from [2,2,2,2] in 6 ways.
5858

5959
```
6060

61-
### **...**
62-
63-
```
64-
61+
### **C++**
62+
63+
```cpp
64+
class Solution {
65+
public:
66+
int threeSumMulti(vector<int>& arr, int target) {
67+
unordered_map<int, long> c;
68+
for (int a : arr) c[a]++;
69+
long res = 0;
70+
for (auto it : c)
71+
for (auto it2 : c) {
72+
int i = it.first, j = it2.first, k = target - i - j;
73+
if (!c.count(k)) continue;
74+
if (i == j && j == k)
75+
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
76+
else if (i == j && j != k)
77+
res += c[i] * (c[i] - 1) / 2 * c[k];
78+
else if (i < j && j < k)
79+
res += c[i] * c[j] * c[k];
80+
}
81+
return res % int(1e9 + 7);
82+
}
83+
};
6584
```
6685
6786
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int threeSumMulti(vector<int>& arr, int target) {
4+
unordered_map<int, long> c;
5+
for (int a : arr) c[a]++;
6+
long res = 0;
7+
for (auto it : c)
8+
for (auto it2 : c) {
9+
int i = it.first, j = it2.first, k = target - i - j;
10+
if (!c.count(k)) continue;
11+
if (i == j && j == k)
12+
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
13+
else if (i == j && j != k)
14+
res += c[i] * (c[i] - 1) / 2 * c[k];
15+
else if (i < j && j < k)
16+
res += c[i] * c[j] * c[k];
17+
}
18+
return res % int(1e9 + 7);
19+
}
20+
};

0 commit comments

Comments
 (0)