专栏首页SnailTyanLeetcode 349. Intersection of Two Arrays

Leetcode 349. Intersection of Two Arrays

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

1. Description

2. Solution

  • Version 1
class Solution:
    def intersection(self, nums1, nums2):
        result = set()
        for x in nums1:
            if x in nums2:
                result.add(x)
        return list(result)
  • Version 2
class Solution:
    def intersection(self, nums1, nums2):
        result = set()
        stat = {}
        for x in nums1:
            stat[x] = x

        for x in nums2:
            if x in stat:
                result.add(x)

        return list(result)
  • Version 3
class Solution:
    def intersection(self, nums1, nums2):
        return list(set(nums1) & set(nums2))

Reference

  1. https://leetcode.com/problems/intersection-of-two-arrays/

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • leetcode-349-Intersection of Two Arrays

    chenjx85
  • LeetCode 349. Intersection of Two Arrays题目代码代码

    样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].

    desperate633
  • 每天一算:Intersection of Two Arrays

    leetcode上第349号问题:Intersection of Two Arrays

    五分钟学算法
  • 349. 两个数组的交集

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/intersection-of-two-arrays 著...

    lucifer210
  • 【LeetCode 136】 关关的刷题日记33 Intersection of Two

    关关的刷题日记33 – Leetcode 349. Intersection of Two Arrays 题目 Given two arrays, write ...

    WZEARW
  • 【LeetCode 136】 关关的刷题日记34 Intersection of Two Arrays II

    关关的刷题日记34 – Leetcode 350. Intersection of Two Arrays II 题目 Given two arrays, wri...

    WZEARW
  • LeetCode 349:两个数组的交集 Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection.

    爱写bug
  • Golang Leetcode 349. Intersection of Two Arrays.go

    版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89088980

    anakinsun
  • Leetcode 350. Intersection of Two Arrays II

    文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

    Tyan

扫码关注云+社区

领取腾讯云代金券