Python solution


  • 0
    A
    class Solution(object):
        def intersect(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: List[int]
            """
            if not nums1 or not nums2:
                return []
            map = {}
            res = []
            
            for num in nums1:
                if num not in map:
                    map[num]=1
                else:
                    map[num]+=1
            
            for num in nums2:
                if num in map and map[num]>0:
                    res.append(num)
                    map[num]-=1
            return res
    

Log in to reply
 

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