-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFourSumII.php
37 lines (32 loc) · 850 Bytes
/
FourSumII.php
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
<?php
declare(strict_types=1);
namespace leetcode;
class FourSumII
{
public static function fourSumCount(array $a, array $b, array $c, array $d): int
{
if (empty($a) || empty($b) || empty($c) || empty($d)) {
return 0;
}
if (count($a) !== count($b) ||
count($b) !== count($c) ||
count($c) !== count($d) ||
count($d) !== count($a)) {
return 0;
}
[$ans, $map] = [0, []];
foreach ($a as $v1) {
foreach ($b as $v2) {
$sum = $v1 + $v2;
$map[$sum] = ($map[$sum] ?? 0) + 1;
}
}
foreach ($c as $v1) {
foreach ($d as $v2) {
$sum = -($v1 + $v2);
$ans += $map[$sum] ?? 0;
}
}
return $ans;
}
}