350. Intersection of Two Arrays II【E】
2016-05-22 17:11
253 查看
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 =
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 num2'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?
Subscribe to see which companies asked this question
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 num2'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?
Subscribe to see which companies asked this question
class Solution(object): def intersect(self, nums1, nums2): nums1.sort() nums2.sort() res = [] i = 0 j = 0 while i < (len(nums1)) and j < (len(nums2)): #print i,j if nums1[i] == nums2[j]: res += nums1[i], i += 1 j += 1 elif nums1[i] < nums2[j]: i += 1 else: j += 1 return res
相关文章推荐
- Cooperation.GTST团队第二周项目总结
- Android edittext默认不获取焦点
- java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程 序和应用程序之间的体系结构不匹配
- 双联通
- Flux--Hello World
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建
- java的若干问题(1)——继承、多态、抽象类与接口
- 使用Object-C实现23种设计模式之建造者模式
- Tensorflow高层次API----Scikit Flow介绍和源码分析
- 2950 突击考试
- 贝叶斯分类器笔记
- 深度学习(三十五)异构计算GLSL学习笔记(1)
- Hibernate核心思想—ORM机制(一)
- PHP7中php.ini、php-fpm和www.conf的配置
- 第一章
- Lua内存泄露检测原理和weak_table弱引用
- android的ListView的分割线设置为虚线
- Android 获取图片资源id
- Java中的String、StringBuilder以及StringBuffer
- c++实现数组栈和链式栈