FourSumII::fourSumCount()   C
last analyzed

Complexity

Conditions 13
Paths 11

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 27
rs 6.6166
cc 13
nc 11
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class FourSumII
8
{
9
    public static function fourSumCount(array $a, array $b, array $c, array $d): int
10
    {
11
        if (empty($a) || empty($b) || empty($c) || empty($d)) {
12
            return 0;
13
        }
14
        if (count($a) !== count($b) ||
15
            count($b) !== count($c) ||
16
            count($c) !== count($d) ||
17
            count($d) !== count($a)) {
18
            return 0;
19
        }
20
        [$ans, $map] = [0, []];
21
        foreach ($a as $v1) {
22
            foreach ($b as $v2) {
23
                $sum = $v1 + $v2;
24
                $map[$sum] = ($map[$sum] ?? 0) + 1;
25
            }
26
        }
27
28
        foreach ($c as $v1) {
29
            foreach ($d as $v2) {
30
                $sum = -($v1 + $v2);
31
                $ans += $map[$sum] ?? 0;
32
            }
33
        }
34
35
        return $ans;
36
    }
37
}
38