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