diff --git a/solution/0200-0299/0238.Product of Array Except Self/README.md b/solution/0200-0299/0238.Product of Array Except Self/README.md index ac5b705002f89..0d6d19f23387b 100644 --- a/solution/0200-0299/0238.Product of Array Except Self/README.md +++ b/solution/0200-0299/0238.Product of Array Except Self/README.md @@ -229,6 +229,30 @@ public class Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer[] + */ + function productExceptSelf($nums) { + $n = count($nums); + $ans = []; + for ($i = 0, $left = 1; $i < $n; ++$i) { + $ans[$i] = $left; + $left *= $nums[$i]; + } + for ($i = $n - 1, $right = 1; $i >= 0; --$i) { + $ans[$i] *= $right; + $right *= $nums[$i]; + } + return $ans; + } +} +``` + ### **...** ``` diff --git a/solution/0200-0299/0238.Product of Array Except Self/README_EN.md b/solution/0200-0299/0238.Product of Array Except Self/README_EN.md index df54a2193619e..37acc0d1a8e08 100644 --- a/solution/0200-0299/0238.Product of Array Except Self/README_EN.md +++ b/solution/0200-0299/0238.Product of Array Except Self/README_EN.md @@ -211,6 +211,30 @@ public class Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer[] + */ + function productExceptSelf($nums) { + $n = count($nums); + $ans = []; + for ($i = 0, $left = 1; $i < $n; ++$i) { + $ans[$i] = $left; + $left *= $nums[$i]; + } + for ($i = $n - 1, $right = 1; $i >= 0; --$i) { + $ans[$i] *= $right; + $right *= $nums[$i]; + } + return $ans; + } +} +``` + ### **...** ``` diff --git a/solution/0200-0299/0238.Product of Array Except Self/Solution.php b/solution/0200-0299/0238.Product of Array Except Self/Solution.php new file mode 100644 index 0000000000000..db4bcdf26e658 --- /dev/null +++ b/solution/0200-0299/0238.Product of Array Except Self/Solution.php @@ -0,0 +1,19 @@ +class Solution { + /** + * @param Integer[] $nums + * @return Integer[] + */ + function productExceptSelf($nums) { + $n = count($nums); + $ans = []; + for ($i = 0, $left = 1; $i < $n; ++$i) { + $ans[$i] = $left; + $left *= $nums[$i]; + } + for ($i = $n - 1, $right = 1; $i >= 0; --$i) { + $ans[$i] *= $right; + $right *= $nums[$i]; + } + return $ans; + } +}