SpiralMatrix::spiralOrder2()   C
last analyzed

Complexity

Conditions 13
Paths 7

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 39
rs 6.6166
cc 13
nc 7
nop 1

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 SpiralMatrix
8
{
9
    public static function spiralOrder(array $matrix): array
10
    {
11
        if (empty($matrix) || empty($matrix[0])) {
12
            return $matrix;
13
        }
14
        $ans = [];
15
        [$rowi, $rown] = [0, count($matrix) - 1];
16
        [$coli, $coln] = [0, count($matrix[0]) - 1];
17
        while ($rowi <= $rown && $coli <= $coln) {
18
            // col: i -> n -> [rowi][j]
19
            for ($j = $coli; $j <= $coln; $j++) {
20
                array_push($ans, $matrix[$rowi][$j]);
21
            }
22
            $rowi++;
23
24
            // row: i -> n -> [j][coln]
25
            for ($j = $rowi; $j <= $rown; $j++) {
26
                array_push($ans, $matrix[$j][$coln]);
27
            }
28
            $coln--;
29
30
            if ($rowi <= $rown) {
31
                // col: n -> i -> [rown][j]
32
                for ($j = $coln; $j >= $coli; $j--) {
33
                    array_push($ans, $matrix[$rown][$j]);
34
                }
35
            }
36
            $rown--;
37
38
            if ($coli <= $coln) {
39
                // row: n -> i -> [j][coln]
40
                for ($j = $rown; $j >= $rowi; $j--) {
41
                    array_push($ans, $matrix[$j][$coli]);
42
                }
43
            }
44
            $coli++;
45
        }
46
47
        return $ans;
48
    }
49
50
    public static function spiralOrder2(array $matrix): array
51
    {
52
        if (empty($matrix) || empty($matrix[0])) {
53
            return $matrix;
54
        }
55
        [$ans, $dir] = [[], 0];
56
        [$rowi, $rown] = [0, count($matrix) - 1];
57
        [$coli, $coln] = [0, count($matrix[0]) - 1];
58
        while ($rowi <= $rown && $coli <= $coln) {
59
            switch ($dir) {
60
                case 0:
61
                    for ($col = $coli; $col <= $coln; $col++) {
62
                        array_push($ans, $matrix[$rowi][$col]);
63
                    }
64
                    $rowi++;
65
                    break;
66
                case 1:
67
                    for ($row = $rowi; $row <= $rown; $row++) {
68
                        array_push($ans, $matrix[$row][$coln]);
69
                    }
70
                    $coln--;
71
                    break;
72
                case 2:
73
                    for ($col = $coln; $col >= $coli; $col--) {
74
                        array_push($ans, $matrix[$rown][$col]);
75
                    }
76
                    $rown--;
77
                    break;
78
                case 3:
79
                    for ($row = $rown; $row >= $rowi; $row--) {
80
                        array_push($ans, $matrix[$row][$coli]);
81
                    }
82
                    $coli++;
83
                    break;
84
            }
85
            $dir = ($dir + 1) % 4;
86
        }
87
88
        return $ans;
89
    }
90
}
91