letcode 两个数组求交集,哈希解法

class Solution(object):
def intersect(self, nums1, nums2):

    if len(nums1) > len(nums2):
        return self.intersect(nums2, nums1)
    
    m = collections.Counter()
    for num in nums1:
        m[num] += 1
    
    intersection = list()
    for num in nums2:
        if m.get(num) > 0:
            intersection.append(num)
            m[num] -= 1
            if m[num] == 0:
                m.pop(num)
    
    return intersection

posted @ 2021-04-01 10:13  谁动了我的奶盖  阅读(42)  评论(0编辑  收藏  举报