8000 Sync LeetCode submission - 3Sum With Multiplicity (java) · thatbeautifuldream/leetcode-sync@7b9734d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b9734d

Browse files
Sync LeetCode submission - 3Sum With Multiplicity (java)
1 parent de2d6e5 commit 7b9734d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int threeSumMulti(int[] arr, int target) {
3+
int mod = 1_000_000_007;
4+
long result = 0;
5+
long[] c = new long[101];
6+
for(int i : arr) c[i]++;
7+
for(int i = 0; i <= 100; i++) {
8+
for(int j = i; j <= 100; j++) {
9+
int k = target - i - j;
10+
if(k < 0 || k > 100) continue;
11+
if(i == j && j == k) {
12+
result += (c[i] * (c[i] - 1) * (c[i] - 2) / 6);
13+
}
14+
else if(i == j && j != k) {
15+
result += ((c[i] * (c[i] - 1) / 2) * c[k]);
16+
}
17+
else if(i < j && j < k) {
18+
result += (c[i] * c[j] * c[k]);
19+
}
20+
}
21+
}
22+
return (int)(result%mod);
23+
}
24+
}

0 commit comments

Comments
 (0)
0