Skip to content

Commit 87ae0c8

Browse files
committedDec 15, 2020
feat: intersection-of-two-arrays-ii by map
1 parent f92e96b commit 87ae0c8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
 

‎arr.intersection-of-two-arrays-ii.py

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ class Solution:
88
给定两个数组,编写一个函数来计算它们的交集。
99
"""
1010
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
11+
# 循环遍历
12+
res = []
13+
tmp = {}
14+
for i in nums1:
15+
tmp[i] = tmp.get(i, 0) + 1
16+
17+
for j in nums2:
18+
if j in tmp.keys() and tmp[j] > 0:
19+
res.append(j)
20+
tmp[j] -= 1
21+
22+
return res
23+
24+
def intersectByForce(self, nums1: List[int], nums2: List[int]) -> List[int]:
1125
# 循环遍历
1226
res = []
1327
for a in nums1:

0 commit comments

Comments
 (0)
Please sign in to comment.