LeetCode 349 - Intersection of Two Arrays - Python

preview_player
Показать описание
Solution, explanation, and complexity analysis for LeetCode 349 in Python

Problem Description:

Code:

Рекомендации по теме
Комментарии
Автор

Thanks for sharing multiple versions, my first attempt I followed an over complicated version of the two set algo (checking which set was shorter and iterating through it), but I'm surprised the fastest version in ms seems to be this one:

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

for n in nums2:
if n in set1:
ans.append(n)
set1.remove(n)
if not set1:
break

return ans

juanmacias
welcome to shbcf.ru