LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/
题目:
Given two arrays, write a function to compute their intersection.
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?
题解:
可以使用双指针.
Time Complexity: O(nlogn). Space: O(1).
AC Java:
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0;
int j = 0;
List<Integer> res = new ArrayList<Integer>();
while(i<nums1.length && j<nums2.length){
if(nums1[i] < nums2[j]){
i++;
}else if(nums1[i] > nums2[j]){
j++;
}else{
res.add(nums1[i]);
i++;
j++;
}
}
int [] resArr = new int[res.size()];
int k = 0;
for(int num : res){
resArr[k++] = num;
}
return resArr;
}
}
Could use HashMap as well.
Time Complexity: O(m + n).
Space: O(Math.min(m, n)).
AC Java:
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if(nums1 == null || nums2 == null){
return new int[0];
}
HashMap<Integer, Integer> hm = new HashMap<>();
for(int num : nums1){
hm.put(num, hm.getOrDefault(num, 0) + 1);
}
List<Integer> res = new ArrayList<>();
for(int num : nums2){
if(hm.containsKey(num)){
res.add(num);
if(hm.get(num) == 1){
hm.remove(num);
}else{
hm.put(num, hm.get(num) - 1);
}
}
}
int [] resArr = new int[res.size()];
int i = 0;
for(int num : res){
resArr[i++] = num;
}
return resArr;
}
}
Follow up 2 nums1 length is smaller. 用双指针先sort两个array明显没有利用到num1.length小的特性. 若是用HashMap来记录num1每个element出现频率再iterate nums2, 那么Time Complexity: O(m + n), m = nums1.length, n = num2.length. Space: O(m).
或者sort nums1 再对每一个num2的element在 sorted nums1上做 binary search. Time Complexity: O(mlogm + nlogm). Space: O(1).
由此可见,当m很小时,用HashMap和binary search就是time和space的trade off.
Follow up 3 nums2 is sorted but too big for memory. I/O的操作很贵,所以首先想到的是避免I/O的次数。
若是nums1可以全部load到memory上, 先sort nums1再把nums2从小到大分开load到memory来.
if load进来这一段最大值, 也就是最后一个值<nums1[0] 或者 load进来这一段最小值, 也就是第一个值>nums1[nums1.length-1]可以直接跳过, load下一段. else load进来这一段 和 sorted nums1做双指针.
若是nums1也太大了,就先external sort nums1, 在分开load进来nums1一段和nums2一段做双指针.
类似Find Common Characters, Intersection of Two Arrays.
LeetCode Intersection of Two Arrays II的更多相关文章
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- LeetCode_350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...
随机推荐
- Python学习笔记(一)——环境搭建
一.安装包下载: 国内镜像:32位:http://pan.baidu.com/s/1jI4q4lS 64位:http://pan.baidu.com/s/1eRPhpRW 版本更迭速度很 ...
- SQL Server 数据库巡检脚本
--1.查看数据库版本信息 select @@version --2.查看所有数据库名称及大小 exec sp_helpdb --3.查看数据库所在机器的操作系统参数 exec master..xp_ ...
- Jmeter之Web端HTTP性能测试(九)
之前有跟大家讲过通过Badboy来录制脚本,这里就不多说了,需要的可以参考 Jmeter之Badboy录制脚本及简化脚本http请求(三) 这边就不用项目的链接了,直接采用http://www.cnb ...
- Android Studio 之 no render target selected
今天第一次使用android studio, 莫名其妙出现 no render target selected的错误,没有设计界面, 各种百度之后在 stackoverflow.com/questio ...
- 【CentOS】磁盘管理与vim编译器
一.查看硬盘或目录容量 1.df [-hmkiT] -h 查看系统磁盘使用情况 -m 使用MBytes显示结果 -k 使用KBytes显示结果 -i 查看inode -T 查看Type 2 ...
- php中session原理及安全性问题
有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制 我们先简单的了解一些http的知识,从而理解该协议 ...
- 【前端开发】优化代码之减少引入,css预编译语言的优点,stylus的使用
前言:我必须得承认在最最最开始的时候,我对于css的预编译是非常不以为然的,这是错误的.一般在页面编写过程中,我会将需要reset的css放在reset.css中,讲会需要重复用到的放置到public ...
- 【转】如何提高意志力&如何坚持每天学习
第一篇如何提高意志力 有一种品质可以使一个人在碌碌无为的平庸之辈中脱颖而出,这个品质不是天资,不是教育,也不是智商,而是自律.有了自律,一切皆有可能,无,则连最简单的目标都显得遥不可及.–西奥多·罗斯 ...
- Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...
- PHP+Mysql+easyui点击左侧tree菜单对应表名右侧动态生成datagrid加载表单数据(二)
关于tree菜单生成,参考我的另一篇博文地址tree 菜单 实现功能:点击左侧tree菜单中的table,右侧通过datagrid加载出该表对用的所有数据 难点:获取该表的所有列名,动态生成datag ...