Intersection of Two Arrays II
class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
#hashmap的index为nums的值,value为值出现的个数
hashmap = {}
result = []
for num in nums1:
if num in hashmap:
hashmap[num] += 1
else:
hashmap[num] = 1
for num in nums2:
if num in hashmap and hashmap[num] > 0:
result.append(num)
hashmap[num] -= 1
return result二刷:
hashmap:
Two Pointers:
Last updated