Skip to content

Commit 861ba7a

Browse files
committed
349.intersection-of-two-arrays
1 parent 1a1e378 commit 861ba7a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This is a **continually updated, open source** project.
2727
| Problem | Solution | Time | Difficulty | Tag | Note|
2828
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
2929
| 88.merge-sorted-array | [python](./algorithm/88.merge-sorted-array.py) | O(N + M) | Easy |
30+
| 349.intersection-of-two-arrays | [python](./algorithm/349.intersection-of-two-arrays.py) O(N + M) |Easy
3031

3132
## Two Pointers
3233

@@ -35,7 +36,7 @@ This is a **continually updated, open source** project.
3536
| 19.remove-nth-node-from-end-of-list | [python](./algorithm/19.remove-nth-node-from-end-of-list.py) | O(N) | Medium
3637
| 141.linked-list-cycle | [python](./algorithm/141.linked-list-cycle.py) | O(N) | Easy | linked-list
3738
| 142.linked-list-cycle-ii | [python](./algorithm/142.linked-list-cycle-ii.py) | O(N) | Medium | linked-list
38-
| 160.intersection-of-two-linked-lists | [python](./algorithm/160.intersection-of-two-linked-lists.py) | O(N+M) | Easy | linked-list
39+
| 160.intersection-of-two-linked-lists | [python](./algorithm/160.intersection-of-two-linked-lists.py) | O(N + M) | Easy | linked-list
3940

4041

4142
## Linked List
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def intersection(self, nums1, nums2):
3+
"""
4+
:type nums1: List[int]
5+
:type nums2: List[int]
6+
:rtype: List[int]
7+
"""
8+
9+
# 1. use a set to test if exist O(N + M)
10+
# 2. use binary search to find if exist O(NlogN) + O(MlogN) = O((N + M) * logN)
11+
# 3. sort left and right, and merge it O(NLogN) + O(MlogM) + min(M, N)
12+
13+
set1 = set(nums1)
14+
set2 = set(nums2)
15+
16+
return list(set1.intersection(set2))

0 commit comments

Comments
 (0)