my python solution used with dictionary


  • 0
    class Solution(object):
        def intersect(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: List[int]
            """
            if len(nums1)<len(nums2):
                nums1,nums2=nums2,nums1
    
            set1,set2,set3={},{},{}           
            for var in nums1:
                if var not in set1.keys():
                    set1[var]=1
                else:
                    set1[var]+=1
            for var in nums2:
                if var not in set2.keys():
                    set2[var]=1
                else:
                    set2[var]+=1
    
            for var in set2.keys():
                if var in set1.keys():
                    set3[var]=min(set2[var],set1[var])
            res=[x for x in set3.keys() for i in range(set3[x])]
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.