Published:
Updated:

  • λ‚˜μ²˜λŸΌ Hash Table λ°©μ‹μœΌλ‘œ ν•˜λŠ” 게 Two Pointer λ°©μ‹μœΌλ‘œ ν•˜λŠ” 것보닀 더 μ‰¬μš΄ 것 κ°™λ‹€.


Solution - Brute Force, $O(N^2)$

from typing import List


class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        answer = set()

        for n1 in nums1:
            for n2 in nums2:
                if n1 == n2:
                    answer.add(n1)

        return list(answer)

Solution - Hash Table, $O(N)$

from typing import List


class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        answer = set()

        num1_dic = {n1 for n1 in nums1}
        for n2 in nums2:
            if n2 in num1_dic:
                answer.add(n2)

        return answer


Reference

Leave a comment