(python版) Leetcode-349.两个数组的交集

01 题目

链接:https://leetcode-cn.com/problems/intersection-of-two-arrays
给定两个数组,编写一个函数来计算它们的交集。

示例 1:

输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]

示例 2:

输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]

02 解析

我用(python版) Leetcode-350.两个数组的交集 II的一行Counter竟然报错?

集合(set)是一个无序的不重复元素序列。可以使用大括号 { } 或者 set() 函数创建集合,

>>> a = set('abracadabra')
>>> a                                  
{
     'a', 'r', 'b', 'c', 'd'}

03 代码

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        s1 = set(nums1)
        s2 = set(nums2)
        return list(s1&s2)

你可能感兴趣的:(Leetcode刷题系列,python,leetcode)