- ๋์ฒ๋ผ Hash Table ๋ฐฉ์์ผ๋ก ํ๋ ๊ฒ Two Pointer ๋ฐฉ์์ผ๋ก ํ๋ ๊ฒ๋ณด๋ค ๋ ์ฌ์ด ๊ฒ ๊ฐ๋ค.
Solution - Brute Force, O(N2)
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