Counter计数:如果nums2中的元素在counter中计数大于0,则添加到res中,并且计数减1;
class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
from collections import Counter
dict_nums1 = Counter(nums1)
res = []
for i in nums2:
if dict_nums1[i]>0:
res.append(i)
dict_nums1[i] -= 1
return res
网友评论