Leetcode350 Intersection of Two Arrays II &Leetcode Intersection of Two Arrays
来源:互联网 发布:胡克霍根身体数据 编辑:程序博客网 时间:2024/06/05 12:46
Given two arrays, write a function to compute their intersection.
C(nums1)&C(nums2)
Out[3]: Counter({2: 2, 3: 1})
时间复杂度:O(n)
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- 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?
题意:求两个数组的交集
解法:
击败了60%+的答案
class Solution(object): def intersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ C = collections.Counter return list((C(nums1) & C(nums2)).elements()) #&代表求交集运算
这个解法的牛逼在于~惭愧,我第一次知道有Collections这个类
Counter是Collections这个类的一个子类,详情可参考 http://www.pythoner.com/205.html
例子:
import collectionsnums1=[3,1,4,4,2,2,]nums2=[3,3,5,6,2,2,2]C = collections.Counter
C(nums1)
Out[1]: Counter({1: 1, 2: 2, 3: 1, 4: 2})
Out[1]: Counter({1: 1, 2: 2, 3: 1, 4: 2})
C(nums2)
Out[2]: Counter({2: 3, 3: 2, 5: 1, 6: 1})
Out[2]: Counter({2: 3, 3: 2, 5: 1, 6: 1})
C(nums1)&C(nums2)
Out[3]: Counter({2: 2, 3: 1})
list((C(nums1)&C(nums2)).elements())
Out[11]: [2, 2, 3]
Out[11]: [2, 2, 3]
这道题里最快的答案:
class Solution(object): def intersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ maps={} for n in nums1: if n in maps: maps[n]+=1 else: maps[n]=1 result=[] for i in range(len(nums2)): if nums2[i] in maps and maps[nums2[i]]>0:#>0其实是在取交集 result.append(nums2[i]) maps[nums2[i]]-=1 return result
时间复杂度:O(n)
思路:对于nums1,把 “数字-出现次数“这个键值对保存在maps字典里
然后遍历nums2,如果nums2的某数字出现在maps1里且maps里该数字剩余的出现次数>0,就表示对于nums2里的该数字而言,它是与nums1的交集,所以放到结果result列里。然后maps[]里把该数字的剩余出现次数-1(代表该数字已经放在结果集里/代表nums2里的该数字已经被考虑过)
Leetcode349 Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ nums1=set(nums1) nums2=set(nums2) return list(nums1&nums2)
注意 & 其实已经是求交集了,但是对两个set求交集,结果仍是set
要求返回list, 所以list()这样强制类型转换
解法二:我自己参考上题的解法 ,不用set()函数(超级慢,only beat 5.38%的用户)
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ maps=[] for n in nums1: if n not in maps: maps.append(n) result=[] for i in range(len(nums2)): if nums2[i] in maps and nums2[i] not in result: result.append(nums2[i]) return result
阅读全文
0 0
- Leetcode350. Intersection of Two Arrays II
- Leetcode350. Intersection of Two Arrays II
- LeetCode350. Intersection of Two Arrays II
- leetcode350~Intersection of Two Arrays II
- leetcode350. Intersection of Two Arrays II
- Leetcode350 Intersection of Two Arrays II &Leetcode Intersection of Two Arrays
- Leetcode350 - Intersection of Two Arrays II(two pointers)
- Leetcode350. Intersection of Two Arrays II很简单的做法
- [LeetCode] Intersection of Two Arrays II
- LeetCode Intersection of Two Arrays II
- LeetCode 350. Intersection of Two Arrays II
- LeetCode:Intersection of Two Arrays II
- leetcode.350. Intersection of Two Arrays II
- LeetCode 350:Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- leetcode Intersection of Two Arrays II
- 18、使用 tf.app.flags 接口定义命令行参数
- C语言面向对象编程之一:封装与继承
- [Yii2 Widget]FancytreeWidget树状结构
- 19、TensorFlow 实现最近邻分类器(K=1)
- 过滤掉字符串中的非数字
- Leetcode350 Intersection of Two Arrays II &Leetcode Intersection of Two Arrays
- C语言面向对象编程之二:继承
- 20、TensorFlow 中 MNIST 数据集的使用
- 中山大学算法课程题目详解(第九周)
- TextView 跑马灯
- 自定义类型与内存对齐
- 一文读懂卷积神经网络
- JavaScript实现排序和搜索
- dubbo hello world