给你三个整数数组 nums1
、nums2
和 nums3
,请你构造并返回一个 元素各不相同的 数组,且由 至少 在 两个 数组中出现的所有值组成。数组中的元素可以按 任意 顺序排列。
示例 1:
示例 2:
示例 3:
提示:
1 <= nums1.length, nums2.length, nums3.length <= 100
1 <= nums1[i], nums2[j], nums3[k] <= 100
题目来源:https://leetcode.cn/problems/two-out-of-three/description/
解题方法:
class Solution {
/**
* @param Integer[] $nums1
* @param Integer[] $nums2
* @param Integer[] $nums3
* @return Integer[]
*/
function twoOutOfThree($nums1, $nums2, $nums3) {
// $nums1和$nums2的交集并去重
$intersect_one = array_unique(array_intersect($nums1, $nums2));
// 同上
$intersect_two = array_unique(array_intersect($nums1, $nums3));
// 同上
$intersect_three = array_unique(array_intersect($nums2, $nums3));
// 拼接后去重
$res = array_unique(array_merge($intersect_one, $intersect_two, $intersect_three));
return $res;
}
}