letcode 两个数组求交集,哈希解法
2021-04-02 00:29
标签:solution count src obj 交集 return def ret class class Solution(object): letcode 两个数组求交集,哈希解法 标签:solution count src obj 交集 return def ret class 原文地址:https://www.cnblogs.com/Ann21/p/14605065.html
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
下一篇:Java基础回顾
文章标题:letcode 两个数组求交集,哈希解法
文章链接:http://soscw.com/index.php/essay/71155.html