leetcode NO.349 两个数组的交集 (python实现)

来源

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

题目描述

给定两个数组,写一个函数来计算它们的交集。
例子:
给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].
提示:
每个在结果中的元素必定是唯一的。
我们可以不考虑输出结果的顺序。

代码实现

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        nums = set(nums1) & set(nums2)
        if nums:
            nums = list(nums)
        else:
            nums = []
        return nums

猜你喜欢

转载自www.cnblogs.com/everfight/p/leetcode_349.html
今日推荐