350. Intersection of Two Arrays II(python)

  1. class Solution(object):  
  2.     def intersect(self, nums1, nums2):  
  3.         """ 
  4.         :type nums1: List[int] 
  5.         :type nums2: List[int] 
  6.         :rtype: List[int] 
  7.         """  
  8.         res = []  
  9.         num = {}  
  10.           
  11.         for c in nums1:  
  12.             num[c] = num[c]+1 if c in num else 1  
  13.           
  14.         for j in nums2:  
  15.             if j in num and num[j]>0:  
  16.                 res.append(j)  
  17.                 num[j]-=1  
  18.                   
  19.         return res  

猜你喜欢

转载自blog.csdn.net/weixin_41362649/article/details/79975000