Skip to content

Commit 9dc2a38

Browse files
committed
350.intersection-of-two-arrays-ii
1 parent 861ba7a commit 9dc2a38

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This is a **continually updated, open source** project.
2828
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
2929
| 88.merge-sorted-array | [python](./algorithm/88.merge-sorted-array.py) | O(N + M) | Easy |
3030
| 349.intersection-of-two-arrays | [python](./algorithm/349.intersection-of-two-arrays.py) O(N + M) |Easy
31+
| 350.intersection-of-two-arrays-ii | [python](./algorithm/350.intersection-of-two-arrays-ii.py) O(N * logN) |Easy
3132

3233
## Two Pointers
3334

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def intersect(self, nums1, nums2):
3+
"""
4+
:type nums1: List[int]
5+
:type nums2: List[int]
6+
:rtype: List[int]
7+
"""
8+
9+
nums1.sort()
10+
nums2.sort()
11+
results = []
12+
left = 0
13+
right = 0
14+
while (left < len(nums1) and right < len(nums2)):
15+
if nums1[left] == nums2[right]:
16+
results.append(nums1[left])
17+
left += 1
18+
right += 1
19+
elif nums1[left] > nums2[right]:
20+
right += 1
21+
else:
22+
left += 1
23+
24+
return results

0 commit comments

Comments
 (0)