【Leetcode】349. Intersection of Two Arrays

class Solution1(object):
    def intersection(self, nums1, nums2):
        """
        use set intersection
        """
        return list(set(nums1).intersection(set(nums2)))

class Solution2(object):
    def intersection(self, nums1, nums2):
        """
        use dictionary, dictionary used to store num already appear to avoid redundancy
        """
        dict1, dict2 = {}, {}
        result = []
        for num in nums1:
            dict1[num] =1
        for num in nums2:
            if num in dict1 and num not in dict2:
                result.append(num)
            dict2[num] =1
        return result

相关文章:

  • 2022-12-23
  • 2021-08-15
  • 2021-10-03
  • 2021-04-12
  • 2022-02-17
猜你喜欢
  • 2021-10-28
  • 2021-07-08
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案