-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0349_intersection_arrays.py
More file actions
34 lines (30 loc) · 1.02 KB
/
0349_intersection_arrays.py
File metadata and controls
34 lines (30 loc) · 1.02 KB
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
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
# Length 0
if (len(nums1) == 0 or len(nums2) == 0):
return []
# Length 1
if (len(nums1) == 1):
if (nums1[0] in nums2):
return nums1
if (len(nums2) == 1):
if (nums2[0] in nums1):
return nums2
if (len(nums1) == 1 or len(nums2) == 1):
return []
intersect = []
# Loop nums1
if (len(nums1) < len(nums2)):
for num in nums1:
if (num in nums2):
if (num not in intersect):
intersect.append(num)
nums2.remove(num)
# Loop nums2
else:
for num in nums2:
if (num in nums1):
if (num not in intersect):
intersect.append(num)
nums1.remove(num)
return intersect