統(tǒng)一企業(yè)信息管理系統(tǒng)網(wǎng)站seo博客網(wǎng)站
給你三個(gè)整數(shù)數(shù)組 nums1
、nums2
和 nums3
,請(qǐng)你構(gòu)造并返回一個(gè) 元素各不相同的 數(shù)組,且由 至少 在 兩個(gè) 數(shù)組中出現(xiàn)的所有值組成。數(shù)組中的元素可以按 任意 順序排列。
示例 1:
輸入:nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
輸出:[3,2]
解釋:至少在兩個(gè)數(shù)組中出現(xiàn)的所有值為:
3 ,在全部三個(gè)數(shù)組中都出現(xiàn)過。
2 ,在數(shù)組 nums1 和 nums2 中出現(xiàn)過。
示例 2:
輸入:nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
輸出:[2,3,1]
解釋:至少在兩個(gè)數(shù)組中出現(xiàn)的所有值為:
2 ,在數(shù)組 nums2 和 nums3 中出現(xiàn)過。
3 ,在數(shù)組 nums1 和 nums2 中出現(xiàn)過。
1 ,在數(shù)組 nums1 和 nums3 中出現(xiàn)過。
示例 3:
輸入:nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
輸出:[]
解釋:不存在至少在兩個(gè)數(shù)組中出現(xiàn)的值。
提示:
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;}
}