[Algorithm] 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Not good enough approach
var intersect = function(nums1, nums2) {
let r = [];
// get larger array
const len1 = nums1.length;
const len2 = nums2.length; // larger & smaller
const larger = len1 > len2 ? nums1: nums2;
const smaller = len1 > len2 ? nums2: nums1; // conver larger array to object
let hashed = {};
for (let n of larger) {
if (n in hashed) {
hashed[n]++;
} else {
hashed[n] = 1;
}
} // loop over smaller array
for (let n of smaller) {
if (`${n}` in hashed) {
r.push(n);
hashed[n] = hashed[n]-1;
if (hashed[n] === 0) {
delete hashed[n];
}
}
} return r;
};
The reason that code above is not good enough is because, \
1. we use larger array as lookup, this cause more memory usage. -- actually we need to use smaller array as lookup
2. we use 'len1, len2, samller, larger' extra storage, we can actully swap nums1 and nums by one extra function call.
var intersect = function(nums1, nums2) { if (nums1.length > nums2.length) {
return intersect(nums2, nums1);
} // conver samller array to object
let hashed = {};
for (let n of nums2) {
if (n in hashed) {
hashed[n]++;
} else {
hashed[n] = 1;
}
} let r = [];
// loop over smaller array
for (let n of nums1) {
if (hashed[n] > 0) {
r.push(n);
hashed[n] = hashed[n]-1;
}
} return r;
}
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?
Will write another post for the follow up questions.
[Algorithm] 350. Intersection of Two Arrays II的更多相关文章
- 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] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- leetcode349 350 Intersection of Two Arrays & II
""" Intersection of Two Arrays Given two arrays, write a function to compute their in ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- Sitecore 8.2 工作流程
假设您的新Sitecore项目的所有开发都已完成.现在的下一步是在网站上填写内容并准备上线.客户通知您他们希望使用专门的网站管理员团队负责整个内容管理流程,并要求您为他们准备实例以便能够执行此操作. ...
- sql语句递归查询(start with)
在做项目中遇到一个问题,就是同一个表中的数据存在级联关系,但是只要查出来的末级数据,纠结了好久,好不容易找到了一个博主的分享,在这里做个记录,也是和大家一起分享学习一下这位大神的技术,共勉 写代码时碰 ...
- myeclipse安装android开发环境全过程
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/liang_824/article/det ...
- Windows和Linux简单命令的总结
MS-DOS 命令提示符(cmd) 启动: Win+R,输入cmd回车 切换盘符 盘符名称: 进入文件夹 cd ...
- 整理:WPF中应用附加事件制作可以绑定命令的其他事件
原文:整理:WPF中应用附加事件制作可以绑定命令的其他事件 目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton.MouseDouble等等 一.定义属于Control的附加事 ...
- 封装:Cmd命令调用和常用命令
原文:封装:Cmd命令调用和常用命令 一.Cmd命令调用方法 1.静态方法调用 class Program { static void Main(string[] args) { // Todo :打 ...
- SAP SD 信用检查相关
SAP系统信用管理功能的介绍: R/3系统具有强大的信用管理功能.系统可将来自于FI.SD的财务及销售信息进行汇总, 提供即时的信用数据;并可依据信用政策对订单及发货进行管理,有效地降低风险;并 ...
- thinkPHP中 query()和execute()的区别
query()执行的是查询(select)的SQL语句. execute()执行的是插入(insert)和修改(update)的SQL语句.execute()方法将返回影响的记录数. 如果在TP中使用 ...
- 第一章 Maven 安装配置
Maven基于(POM)项目对象模型,通过一小段描述信息来管理项目的构建.文档.和报告的项目管理软件,类似于php 的管理构建工具composer. 有关详细的Maven学习,可以参考学习https: ...
- 2019 中手游java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.中手游等公司offer,岗位是Java后端开发,因为发展原因最终选择去了中手游,入职一年时间了,也成为了面试官 ...