题解 | #两个数组的交集#

两个数组的交集

http://www.nowcoder.com/practice/56ea71d1f4e94de2aaec10e985874cce

哈希表,利用两个 dict 求出两个数组包含的元素(在 Python 中也可以直接用 set), 再比较 key 集合

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param nums1 int整型一维数组 
# @param nums2 int整型一维数组 
# @return int整型一维数组
#
from collections import Counter
class Solution:
    def intersection(self , nums1: List[int], nums2: List[int]) -> List[int]:
        # write code here
        count_1, count_2 = Counter(nums1), Counter(nums2)
        return list(count_1.keys() & count_2.keys())
class Solution:
    def intersection(self , nums1: List[int], nums2: List[int]) -> List[int]:
        # write code here
        return list(set(nums1) & set(nums2))
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:16
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
线性袋鼠:别听牛客上一帮伪人在那说,小厂不能去,必须去大厂,听他们放屁吧。学院本+一些一本最终的归宿就是中小厂,大厂那么好进吗
我的实习日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务