ThreeSum   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 37
eloc 66
c 2
b 0
f 0
dl 0
loc 107
rs 9.44

3 Methods

Rating   Name   Duplication   Size   Complexity  
C threeSum2() 0 31 14
B threeSum() 0 35 10
C threeSum3() 0 35 13
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ThreeSum
8
{
9
    public static function threeSum(array $nums): array
10
    {
11
        [$ans, $n] = [[], count($nums)];
12
        if (empty($nums) || $n < 3) {
13
            return $ans;
14
        }
15
16
        $isSameArray = static function (array $needle, array $haystack = []) {
17
            sort($needle);
18
            foreach ($haystack as $value) {
19
                sort($value);
20
                if ($needle === $value) {
21
                    return true;
22
                }
23
            }
24
25
            return false;
26
        };
27
28
        for ($i = 0; $i < $n; $i++) {
29
            for ($j = $i + 1; $j < $n; $j++) {
30
                for ($k = $j + 1; $k < $n; $k++) {
31
                    if ($nums[$i] + $nums[$j] + $nums[$k] === 0) {
32
                        $needle = [$nums[$i], $nums[$j], $nums[$k]];
33
                        sort($needle);
34
                        if ($isSameArray($needle, $ans)) {
35
                            continue;
36
                        }
37
                        $ans[] = $needle;
38
                    }
39
                }
40
            }
41
        }
42
43
        return array_reverse($ans);
44
    }
45
46
    public static function threeSum2(array $nums): array
47
    {
48
        [$ans, $n] = [[], count($nums)];
49
        if (empty($nums) || $n < 3) {
50
            return $ans;
51
        }
52
        sort($nums);
53
        for ($i = 0; $i < $n - 2; $i++) {
54
            if ($i === 0 || ($i > 0 && $nums[$i] !== $nums[$i - 1])) {
55
                [$low, $high, $sum] = [$i + 1, $n - 1, -$nums[$i]];
56
                while ($low < $high) {
57
                    if ($nums[$low] + $nums[$high] === $sum) {
58
                        $ans[] = [$nums[$i], $nums[$low], $nums[$high]];
59
                        while ($low < $high && $nums[$low] === $nums[$low + 1]) {
60
                            $low++;
61
                        }
62
                        while ($low < $high && $nums[$high] === $nums[$high - 1]) {
63
                            $high--;
64
                        }
65
                        $low++;
66
                        $high--;
67
                    } elseif ($nums[$low] + $nums[$high] < $sum) {
68
                        $low++;
69
                    } else {
70
                        $high--;
71
                    }
72
                }
73
            }
74
        }
75
76
        return $ans;
77
    }
78
79
    public static function threeSum3(array $nums): array
80
    {
81
        [$ans, $n] = [[], count($nums)];
82
        if (empty($nums) || $n < 3) {
83
            return $ans;
84
        }
85
86
        sort($nums);
87
        for ($i = 0; $i < $n - 2; $i++) {
88
            if ($i > 0 && $nums[$i] === $nums[$i - 1]) {
89
                continue;
90
            }
91
            $low = $i + 1;
92
            $high = $n - 1;
93
            while ($low < $high) {
94
                $sum = $nums[$i] + $nums[$low] + $nums[$high];
95
                if ($sum > 0) {
96
                    $high--;
97
                } elseif ($sum < 0) {
98
                    $low++;
99
                } else {
100
                    $ans[] = [$nums[$i], $nums[$low], $nums[$high]];
101
                    while ($low < $high && $nums[$low] === $nums[$low + 1]) {
102
                        $low++;
103
                    }
104
                    while ($low < $high && $nums[$high] === $nums[$high - 1]) {
105
                        $high--;
106
                    }
107
                    $low++;
108
                    $high--;
109
                }
110
            }
111
        }
112
113
        return $ans;
114
    }
115
}
116