美文网首页算法提高之LeetCode刷题Leetcode模拟面试
[LeetCode][Python]349. 两个数组的交集

[LeetCode][Python]349. 两个数组的交集

作者: bluescorpio | 来源:发表于2019-07-13 16:45 被阅读0次

[LeetCode][Python]349. 两个数组的交集

给定两个数组,编写一个函数来计算它们的交集。

示例 1:

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

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

输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。

思路:关于交集第一反应是使用Set的交集运算&

看了其他人的思路,基本也是这么运算

#! /usr/bin/env python
#coding=utf-8
class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        return list(set(nums1) & set(nums2))

相关文章

网友评论

    本文标题:[LeetCode][Python]349. 两个数组的交集

    本文链接:https://www.haomeiwen.com/subject/fgjmkctx.html