-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection of 2 array II.py
More file actions
33 lines (31 loc) · 970 Bytes
/
Intersection of 2 array II.py
File metadata and controls
33 lines (31 loc) · 970 Bytes
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
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
l1=len(nums1)
l2=len(nums2)
# res=[] # soln1
# if l2<=l1:
# for i in range(l2):
# if nums2[i] in nums1:
# res.append(nums2[i])
# nums1.remove(nums2[i])
# else:
# for i in range(l1):
# if nums1[i] in nums2:
# res.append(nums1[i])
# nums2.remove(nums1[i])
# return res
nums1.sort() # soln2 - optimized
nums2.sort()
res=[]
a=0
b=0
while(a<l1 and b<l2):
if(nums1[a]==nums2[b]):
res.append(nums1[a])
a=a+1
b=b+1
elif(nums1[a]<nums2[b]):
a=a+1
else:
b=b+1
return res