-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZigzagConversion.php
50 lines (44 loc) · 1.21 KB
/
ZigzagConversion.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
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace leetcode;
class ZigzagConversion
{
public static function convert(string $str, int $numRows): string
{
[$m, $n] = [$numRows, strlen($str)];
if ($m <= 1 || $m >= $n) {
return $str;
}
[$s, $k] = ['', 2 * $m - 2];
for ($i = 0; $i < $m; $i++) {
for ($j = $i; $j < $n; $j += $k) {
$s .= $str[$j];
if ($i !== 0 && $i !== $numRows - 1) {
$t = $j + $k - 2 * $i;
if ($t < $n) {
$s .= $str[$t];
}
}
}
}
return $s;
}
public static function convert2(string $str, int $numRows): string
{
[$m, $n] = [$numRows, strlen($str)];
if ($m <= 1 || $m >= $n) {
return $str;
}
[$ans, $key, $step] = [array_fill(0, $m, ''), 0, 1];
for ($i = 0; $i < $n; $i++) {
$ans[$key] .= $str[$i];
if ($key === 0) {
$step = 1;
} elseif ($key === $m - 1) {
$step = -1;
}
$key += $step;
}
return implode('', $ans);
}
}