Test Failed
Push — master ( f38782...f110ed )
by Jinyun
02:42
created

ReverseStringII::reverseStr()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ReverseStringII
8
{
9
    public static function reverseStr(string $s, int $k): string
10
    {
11
        if (empty($s) || $k <= 0) {
12
            return '';
13
        }
14
        $n = strlen($s);
0 ignored issues
show
Unused Code introduced by
The assignment to $n is dead and can be removed.
Loading history...
15
        [$m, $n] = [0, strlen($s)];
16
        for (; $m < $n; $m += 2 * $k) {
17
            for ($i = $m, $j = min($m + $k - 1, $n - 1); $i < $j; $i++, $j--) {
18
                $t = $s[$i];
19
                $s[$i] = $s[$j];
20
                $s[$j] = $t;
21
            }
22
        }
23
24
        return $s;
25
    }
26
}
27