Skip to content

Commit 8216fa6

Browse files
author
weiy
committed
intersection of two arra I_II easy
1 parent ac4e94b commit 8216fa6

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Array/IntersectionOfTwoArrays.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Given two arrays, write a function to compute their intersection.
3+
4+
Example 1:
5+
6+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
7+
Output: [2]
8+
Example 2:
9+
10+
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
11+
Output: [9,4]
12+
Note:
13+
14+
Each element in the result must be unique.
15+
The result can be in any order.
16+
17+
18+
找两个数组里的重复数据。
19+
20+
唯一,任意顺序。
21+
22+
集合,取交集即可。
23+
24+
零启动。
25+
26+
beat 100% 20ms.
27+
28+
测试地址:
29+
https://leetcode.com/problems/intersection-of-two-arrays/description/
30+
31+
"""
32+
class Solution(object):
33+
def intersection(self, nums1, nums2):
34+
"""
35+
:type nums1: List[int]
36+
:type nums2: List[int]
37+
:rtype: List[int]
38+
"""
39+
40+
return list(set(nums1) & set(nums2))

Array/IntersectionOfTwoArraysII.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Given two arrays, write a function to compute their intersection.
3+
4+
Example 1:
5+
6+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
7+
Output: [2,2]
8+
Example 2:
9+
10+
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
11+
Output: [4,9]
12+
Note:
13+
14+
Each element in the result should appear as many times as it shows in both arrays.
15+
The result can be in any order.
16+
Follow up:
17+
18+
What if the given array is already sorted? How would you optimize your algorithm?
19+
What if nums1's size is small compared to nums2's size? Which algorithm is better?
20+
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
21+
22+
与 1 差不多,不过这次有重复。
23+
进阶条件里问:
24+
1. 如果是已排序的可以怎么优化?
25+
2. 如果 数组1 比 数组2 要小,哪个算法好些?
26+
3. 如果 数组2 在硬盘里排过序,但内存不足以一次性全部读取该怎么做?
27+
28+
我的思路是:
29+
1. 先排序。
30+
2. 之后设置两个指针。
31+
32+
若 1与2相同,则将结果添加到最终结果的列表中,1和2各+1。
33+
若 1比2要大,那么表示2向后走还有可能遇到和1相同的数字,所以2 +1。
34+
否则 1 +1。直到有一个到了末尾。
35+
36+
这个思路的话,进阶的1和3直接可以包含进去。
37+
2的话,Discuss 里的其他方法基本上是用 哈希表。 1和2哈希,然后取个数较小的交集,这种方法在数组较小的时候要比上面提到的思路快。
38+
39+
排序后的思路:
40+
41+
beat 100% 24ms.
42+
43+
测试地址:
44+
https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
45+
46+
47+
"""
48+
class Solution(object):
49+
def intersect(self, nums1, nums2):
50+
"""
51+
:type nums1: List[int]
52+
:type nums2: List[int]
53+
:rtype: List[int]
54+
"""
55+
result = []
56+
57+
58+
nums1.sort()
59+
nums2.sort()
60+
61+
_n1 = 0
62+
_n2 = 0
63+
64+
_n1_length = len(nums1)
65+
_n2_length = len(nums2)
66+
67+
while _n1 < _n1_length and _n2 < _n2_length:
68+
if nums1[_n1] == nums2[_n2]:
69+
result.append(nums1[_n1])
70+
_n1 += 1
71+
_n2 += 1
72+
73+
elif nums1[_n1] < nums2[_n2]:
74+
_n1 += 1
75+
else:
76+
_n2 += 1
77+
78+
return result

0 commit comments

Comments
 (0)