From 06651d0b5229a03f3f1cbc22d4ec468630941b23 Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Mon, 10 Jul 2023 11:51:54 +0200 Subject: [PATCH] feat: add php solution to lc problem: No.0238 --- .../README.md | 24 +++++++++++++++++++ .../README_EN.md | 24 +++++++++++++++++++ .../Solution.php | 19 +++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 solution/0200-0299/0238.Product of Array Except Self/Solution.php 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; + } +}