python 两个数组的交集 intersection of two arrays

网友投稿 1296 2022-10-11

python 两个数组的交集 intersection of two arrays

python 两个数组的交集 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] """ setNums1 = set(nums1) setNums2 = set(nums2) result = [] for x in setNums2: if x in setNums1: result.append(x) return result

用set来把list的重复元素过滤掉,然后判断是否存在,把结果保存起来​​​http://waitingfy.com/archives/3724​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Chateau 一个框架用于在任何Android应用程序中添加聊天功能(chateau红酒价格2016)
下一篇:scala map reduce 思想
相关文章

 发表评论

暂时没有评论,来抢沙发吧~