-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersectionOf2Array.py
40 lines (38 loc) · 1.07 KB
/
intersectionOf2Array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
def inter(nums1, nums2):
s1 = set(nums1)
s2 = set(nums2)
return list(s1 & s2)
def listToDic(lis):
dic = {}
for item in lis:
try:
dic[item] += 1
except:
dic[item] = 1
return dic
if inter(nums1, nums2):
d_1 = listToDic(nums1)
d_2 = listToDic(nums2)
re = []
for item in inter(nums1, nums2):
if d_1[item] >= d_2[item]:
for i in range(d_2[item]):
re.append(item)
else:
for i in range(d_1[item]):
re.append(item)
return re
else:
return []
if __name__ == '__main__':
s = Solution()
nums1 = [1]
nums2 = [1,1]
print s.intersection(nums1, nums2)