Leetcode 349. Intersection of Two Arrays

2021-03-02 16:29:32 浏览数 (1)

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

1. Description

2. Solution

  • Version 1
代码语言:javascript复制
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
代码语言:javascript复制
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
代码语言:javascript复制
class Solution:
    def intersection(self, nums1, nums2):
        return list(set(nums1) & set(nums2))

Reference

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

0 人点赞