Conditions | 24 |
Paths | > 20000 |
Total Lines | 38 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
9 | public static function maxKilledEnemies(array $grids): int |
||
10 | { |
||
11 | [$res, $m, $n] = [0, count($grids), count($grids[0])]; |
||
12 | if ($m <= 0 || $n <= 0) { |
||
13 | return $res; |
||
14 | } |
||
15 | $v1 = $v2 = $v3 = $v4 = array_fill(0, $m, array_fill(0, $n, 0)); |
||
16 | for ($i = 0; $i < $m; $i++) { |
||
17 | for ($j = 0; $j < $n; $j++) { // 列从上到下 |
||
18 | $t = ($j === 0 || $grids[$i][$j] === 'W') ? 0 : $v1[$i][$j - 1]; |
||
19 | $v1[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t; |
||
20 | } |
||
21 | for ($j = $n - 1; $j >= 0; $j--) { // 列从下到上 |
||
22 | $t = ($j === $n - 1 || $grids[$i][$j] === 'W') ? 0 : $v2[$i][$j + 1]; |
||
23 | $v2[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t; |
||
24 | } |
||
25 | } |
||
26 | |||
27 | for ($j = 0; $j < $n; $j++) { |
||
28 | for ($i = 0; $i < $m; $i++) { // 行从左到右 |
||
29 | $t = ($i === 0 || $grids[$i][$j] === 'W') ? 0 : $v3[$i - 1][$j]; |
||
30 | $v3[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t; |
||
31 | } |
||
32 | for ($i = $m - 1; $i >= 0; $i--) { // 行从右到左 |
||
33 | $t = ($i === $m - 1 || $grids[$i][$j] === 'W') ? 0 : $v4[$i + 1][$j]; |
||
34 | $v4[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | for ($i = 0; $i < $m; $i++) { |
||
39 | for ($j = 0; $j < $n; $j++) { |
||
40 | if ($grids[$i][$j] === '0') { |
||
41 | $res = max($res, $v1[$i][$j] + $v2[$i][$j] + $v3[$i][$j] + $v4[$i][$j]); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
46 | return $res; |
||
47 | } |
||
79 |