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

两个数组的交集

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-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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