From d443be21c301204c60afe7d99cdc9ec6a7248462 Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Sun, 2 Apr 2023 19:10:32 +0200 Subject: [PATCH] feat add php solution to lc problem: No.0350 --- .../README.md | 25 +++++++++++++++++++ .../README_EN.md | 25 +++++++++++++++++++ .../Solution.php | 20 +++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md index 9d9097bcb1be2..0052672972760 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md @@ -199,6 +199,31 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersect($nums1, $nums2) { + $rs = []; + for ($i = 0; $i < count($nums1); $i++) { + $hashtable[$nums1[$i]] += 1; + } + for ($j = 0; $j < count($nums2); $j++) { + if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { + array_push($rs, $nums2[$j]); + $hashtable[$nums2[$j]] -= 1; + } + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md index 06f53600c244c..d29f596855805 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md @@ -188,6 +188,31 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersect($nums1, $nums2) { + $rs = []; + for ($i = 0; $i < count($nums1); $i++) { + $hashtable[$nums1[$i]] += 1; + } + for ($j = 0; $j < count($nums2); $j++) { + if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { + array_push($rs, $nums2[$j]); + $hashtable[$nums2[$j]] -= 1; + } + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php new file mode 100644 index 0000000000000..fb73f75c413f9 --- /dev/null +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php @@ -0,0 +1,20 @@ +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersect($nums1, $nums2) { + $rs = []; + for ($i = 0; $i < count($nums1); $i++) { + $hashtable[$nums1[$i]] += 1; + } + for ($j = 0; $j < count($nums2); $j++) { + if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { + array_push($rs, $nums2[$j]); + $hashtable[$nums2[$j]] -= 1; + } + } + return $rs; + } +} \ No newline at end of file