mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加 0349.两个数组的交集 PHP版本
This commit is contained in:
@ -209,6 +209,35 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums1
|
||||
* @param Integer[] $nums2
|
||||
* @return Integer[]
|
||||
*/
|
||||
function intersection($nums1, $nums2) {
|
||||
if (count($nums1) == 0 || count($nums2) == 0) {
|
||||
return [];
|
||||
}
|
||||
$counts = [];
|
||||
$res = [];
|
||||
foreach ($nums1 as $num) {
|
||||
$counts[$num] = 1;
|
||||
}
|
||||
foreach ($nums2 as $num) {
|
||||
if (isset($counts[$num])) {
|
||||
$res[] = $num;
|
||||
}
|
||||
unset($counts[$num]);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 相关题目
|
||||
|
||||
* 350.两个数组的交集 II
|
||||
|
Reference in New Issue
Block a user