Leetcode 2032. 至少在两个数组中出现的值

一、题目内容和对应链接

1.题目内容

给你三个整数数组 nums1、nums2 和 nums3 ,请你构造并返回一个 元素各不相同的 数组,且由 至少两个 数组中出现的所有值组成。数组中的元素可以按 任意 顺序排列。

示例 1:

输入:nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
输出:[3,2]
解释:至少在两个数组中出现的所有值为:

  • 3 ,在全部三个数组中都出现过。
  • 2 ,在数组 nums1 和 nums2 中出现过。

示例 2:

输入:nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
输出:[2,3,1]
解释:至少在两个数组中出现的所有值为:

  • 2 ,在数组 nums2 和 nums3 中出现过。
  • 3 ,在数组 nums1 和 nums2 中出现过。
  • 1 ,在数组 nums1 和 nums3 中出现过。

示例 3:

输入:nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
输出:[]
解释:不存在至少在两个数组中出现的值。

提示:

  • 1 <= nums1.length, nums2.length, nums3.length <= 100
  • 1 <= nums1[i], nums2[j], nums3[k] <= 100

2.题目对应链接

Leetcode 2032. 至少在两个数组中出现的值

二、我的想法

  1. 就取交集呗,最后再去重。
class Solution:
    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
        newlist = list()
        newlist.extend(list(set(nums1).intersection(set(nums2))))
        newlist.extend(list(set(nums2).intersection(set(nums3))))
        newlist.extend(list(set(nums1).intersection(set(nums3))))
        return list(set(newlist))
class Solution:
    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
        newlist = list()
        new = [val for val in nums1 if val in nums2]
        newlist.extend(new)
        new = [val for val in nums2 if val in nums3]
        newlist.extend(new)
        new = [val for val in nums1 if val in nums3]
        newlist.extend(new)
        return list(set(newlist))

三、其他人的题解

官方 - 哈希表
ylb - 数组 + 枚举

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值