Published:
Updated:

  • ๋‚˜์ฒ˜๋Ÿผ Hash Table ๋ฐฉ์‹์œผ๋กœ ํ•˜๋Š” ๊ฒŒ Two Pointer ๋ฐฉ์‹์œผ๋กœ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ์‰ฌ์šด ๊ฒƒ ๊ฐ™๋‹ค.


Solution - Brute Force, O(N2)Permalink

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)Permalink

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


ReferencePermalink

Leave a comment