| Total Complexity | 16 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class SortArrayByParityII |
||
| 8 | { |
||
| 9 | public static function sortArrayByParityII(array $nums): array |
||
| 10 | { |
||
| 11 | if (empty($nums)) { |
||
| 12 | return []; |
||
| 13 | } |
||
| 14 | $n = count($nums); |
||
| 15 | [$i, $j] = [0, 1]; |
||
| 16 | while ($i < $n && $j < $n) { |
||
| 17 | while ($i < $n && $nums[$i] % 2 === 0) { |
||
| 18 | $i += 2; |
||
| 19 | } |
||
| 20 | while ($j < $n && $nums[$j] % 2 === 1) { |
||
| 21 | $j += 2; |
||
| 22 | } |
||
| 23 | if ($i < $n && $j < $n) { |
||
| 24 | $tmp = $nums[$i]; |
||
| 25 | $nums[$i] = $nums[$j]; |
||
| 26 | $nums[$j] = $tmp; |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | return $nums; |
||
| 31 | } |
||
| 32 | |||
| 33 | public static function sortArrayByParityII2(array $nums): array |
||
| 53 | } |
||
| 54 | } |
||
| 55 |