【leetcode❤python】350. Intersection of Two Arrays II

#-*- coding: UTF-8 -*-
class Solution(object):
    def intersect(self, nums1, nums2):
        
        if len(nums1)<len(nums2):
            tmp=nums1
            nums1=nums2
            nums2=tmp
       
        tmpdic={}
       
        for num in nums1:
            tmpdic[num]=tmpdic[num]+1 if num in tmpdic else 1
        
        commonList=[]
        for num in nums2:
 
            if tmpdic.get(num)>None and tmpdic.get(num)>=1:
                
                commonList.append(num)
                
                tmpdic[num]=tmpdic.get(num)-1
      
        return commonList
            
sol=Solution()
print sol.intersect(nums1=[1,1,1], nums2=[1,1])   

posted @ 2016-10-12 17:09  火金队长  阅读(118)  评论(0编辑  收藏  举报