| Total Complexity | 9 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class ProductOfArrayExceptSelf |
||
| 8 | { |
||
| 9 | public static function productExceptSelf(array $nums): array |
||
| 10 | { |
||
| 11 | if (empty($nums)) { |
||
| 12 | return []; |
||
| 13 | } |
||
| 14 | $n = count($nums); |
||
| 15 | $ans = $left = $right = array_fill(0, $n, 0); |
||
| 16 | $left[0] = $right[$n - 1] = 1; |
||
| 17 | |||
| 18 | for ($i = 1; $i < $n; $i++) { |
||
| 19 | $left[$i] = $left[$i - 1] * $nums[$i - 1]; |
||
| 20 | } |
||
| 21 | for ($i = $n - 2; $i >= 0; $i--) { |
||
| 22 | $right[$i] = $right[$i + 1] * $nums[$i + 1]; |
||
| 23 | } |
||
| 24 | for ($i = 0; $i < $n; $i++) { |
||
| 25 | $ans[$i] = $left[$i] * $right[$i]; |
||
| 26 | } |
||
| 27 | |||
| 28 | return $ans; |
||
| 29 | } |
||
| 30 | |||
| 31 | public static function productExceptSelf2(array $nums): array |
||
| 48 | } |
||
| 49 | } |
||
| 50 |