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 ...
随机推荐
- execl表格VLOOKUP函数的使用
使用场景:最近在处理一个表格数据的时候出现了一点麻烦.想把另外表2里面对应的的数据放到表1里面,本来表2的ID是乱序的(这里为好看,就顺序排了.),一个个查找复制粘贴比较麻烦,后经大师指点VLOOKU ...
- ****CI框架源码阅读笔记7 配置管理组件 Config.php
http://blog.csdn.net/ohmygirl/article/details/41041597 一个灵活可控的应用程序中,必然会存在大量的可控参数(我们称为配置),例如在CI的主配置文件 ...
- [LeetCode] Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- jsf初学解决GlassFish Server 无法启动
由于公司需要用JSF开发项目.公司同事都不熟悉,本人C# 转JSf.开发工具 netbeans GlassFish. 遇到GlassFish 非常 纠结的问题.搞了好一段时间,, 装好GlassFis ...
- 联合体(union)的使用方法及其本质
转自:http://blog.csdn.net/huqinwei987/article/details/23597091 有些基础知识快淡忘了,所以有必要复习一遍,在不借助课本死知识的前提下做些推理判 ...
- 【转】一台电脑同时运行多个tomcat配置方法
参考:http://blog.csdn.net/zyk906705975/article/details/8471475
- 基于requests实现极客学院课程爬虫
背景 本文主要是为了完成极客学院课程<Python 单线程爬虫>中讲师布置的实战作业. 开发环境 操作系统:windows 10 Python :Python 2.7 IDE:PyChar ...
- SpringMvc的创建流程以及2种加载配置文件的方式
1.首先创建个web项目,第一步导入相应的jar包,并且buildtoPath 2.用elipse或myeclipse点击进入web.xml中 按住 Alt+ / 有个提示 找到前面带 #Dispat ...
- db2look和db2move详解
db2look和db2move简单实例 --- 建库create database db_name on filesystem_location using codeset utf-8 territo ...
- Linux中总线设备驱动模型及平台设备驱动实例
本文将简要地介绍Linux总线设备驱动模型及其实现方式,并不会过多地涉及其在内核中的具体实现,最后,本文将会以平台总线为例介绍设备和驱动程序的实现过程. 目录: 一.总线设备驱动模型总体介绍及其实现方 ...