From 73a1b8bda05470cfca0fa6723465ef19e3556b6d Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Thu, 30 Mar 2023 13:34:38 +0200 Subject: [PATCH] feat: add php solution to lc problem: No.0349 --- .../0349.Intersection of Two Arrays/README.md | 24 +++++++++++++++++++ .../README_EN.md | 24 +++++++++++++++++++ .../Solution.php | 19 +++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 solution/0300-0399/0349.Intersection of Two Arrays/Solution.php diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/README.md b/solution/0300-0399/0349.Intersection of Two Arrays/README.md index 7b7183f95afa0..a1eba31d226f4 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/README.md +++ b/solution/0300-0399/0349.Intersection of Two Arrays/README.md @@ -148,6 +148,30 @@ func intersection(nums1 []int, nums2 []int) []int { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersection($nums1, $nums2) { + $rs = []; + $set1 = array_values(array_unique($nums1)); + $set2 = array_values(array_unique($nums2)); + for ($i = 0; $i < count($set1); $i++) { + $hashmap[$set1[$i]] = 1; + } + for ($j = 0; $j < count($set2); $j++) { + if ($hashmap[$set2[$j]]) array_push($rs, $set2[$j]); + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md b/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md index d06008291a2d5..b420a86e795fc 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md +++ b/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md @@ -136,6 +136,30 @@ func intersection(nums1 []int, nums2 []int) []int { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersection($nums1, $nums2) { + $rs = []; + $set1 = array_values(array_unique($nums1)); + $set2 = array_values(array_unique($nums2)); + for ($i = 0; $i < count($set1); $i++) { + $hashmap[$set1[$i]] = 1; + } + for ($j = 0; $j < count($set2); $j++) { + if ($hashmap[$set2[$j]]) array_push($rs, $set2[$j]); + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php new file mode 100644 index 0000000000000..2366e8d602e43 --- /dev/null +++ b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php @@ -0,0 +1,19 @@ +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersection($nums1, $nums2) { + $rs = []; + $set1 = array_values(array_unique($nums1)); + $set2 = array_values(array_unique($nums2)); + for ($i = 0; $i < count($set1); $i++) { + $hashmap[$set1[$i]] = 1; + } + for ($j = 0; $j < count($set2); $j++) { + if ($hashmap[$set2[$j]]) array_push($rs, $set2[$j]); + } + return $rs; + } +} \ No newline at end of file