-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpiralMatrixII.php
More file actions
62 lines (56 loc) · 1.65 KB
/
SpiralMatrixII.php
File metadata and controls
62 lines (56 loc) · 1.65 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace leetcode;
class SpiralMatrixII
{
public static function generateMatrix(int $n): array
{
if ($n <= 0) {
return [];
}
[$ans, $cnt] = [array_fill(0, $n, array_fill(0, $n, 0)), 1];
[$rowi, $rown] = [0, $n - 1];
[$coli, $coln] = [0, $n - 1];
while ($rowi <= $rown && $coli <= $coln) {
for ($j = $coli; $j <= $coln; $j++) {
$ans[$rowi][$j] = $cnt++;
}
$rowi++;
for ($i = $rowi; $i <= $rown; $i++) {
$ans[$i][$coln] = $cnt++;
}
$coln--;
for ($j = $coln; $j >= $coli; $j--) {
$ans[$rown][$j] = $cnt++;
}
$rown--;
for ($i = $rown; $i >= $rowi; $i--) {
$ans[$i][$coli] = $cnt++;
}
$coli++;
}
return $ans;
}
public static function generateMatrix2(int $n): array
{
if ($n <= 0) {
return [];
}
$ans = array_fill(0, $n, array_fill(0, $n, 0));
$dir = [[0, 1], [1, 0], [0, -1], [-1, 0]];
[$cur, $pos] = [0, [0, -1]];
for ($k = 0; $k < $n * $n; $k++) {
$v = $k + 1;
$i = $pos[0] + $dir[$cur][0];
$j = $pos[1] + $dir[$cur][1];
if (!(($i >= 0 && $i < $n) && ($j >= 0 && $j < $n) && $ans[$i][$j] === 0)) {
$cur = ($cur + 1) % 4;
$i = $pos[0] + $dir[$cur][0];
$j = $pos[1] + $dir[$cur][1];
}
$ans[$i][$j] = $v;
$pos = [$i, $j];
}
return $ans;
}
}