sy1214ei 님의 블로그

[Leet Code] 349. Intersection of Two Arrays - Python 본문

[Coding]

[Leet Code] 349. Intersection of Two Arrays - Python

sy1214ei 2024. 12. 16. 02:37

https://leetcode.com/problems/intersection-of-two-arrays/description/

level : Easy
class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
    	"""
        1. nums1에 있는 값은 모두 True
        2. nums2에 nums1에 있는 값이 있다면 res.add
        3. return list(res) : res에는 num1, num2의 겹치는 값들 다 들어감
        """
        hash_table = {}
        for num in nums1:
            hash_table[num] = True
        result = set()
        for num in nums2:
            if num in hash_table:
                result.add(num)

        return list(result)
        # Time Complexity : O(n)
        # Space Complexity : O(n)