Intersection of Two Arrays
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
return list(set(nums1) & set(nums2))#use dict/hashmap to record all nums appeared in the first list, and then check if there are nums in the second list have appeared in the map.
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
hashmap = {}
result = []
for i in range(len(nums1)):
hashmap[nums1[i]] = i
for num2 in nums2:
if num2 in hashmap and num2 not in result:
result.append(num2)
return result二刷:
Last updated