Python w/ hash table


  • 0
    Z

    class Solution(object):
        def intersect(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: List[int]
            """
            a = nums1
            b = nums2
            a_tbl = {k: a.count(k) for k in set(a)}
            b_tbl = {k: b.count(k) for k in set(b)}
            ret = []
            for k in a_tbl:
                if k in b_tbl:
                    ret += [k]*min(a_tbl[k], b_tbl[k])
            return ret
    

    </code class>


Log in to reply
 

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