[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] ...
随机推荐
- Java-关于接口调用的处理
前言:这是我的第一篇博文,是我对现在一些接口调用的梳理,写的不好,请见谅. 序:接口无非就是“你调用别人的接口”和“别人调用你的接口”,我会对这两种情况分别的理一下我的思路. 准备:我使用的是Http ...
- 【转载】linux 压缩和解压缩命令gz、tar、zip、bz2
linux系统下压缩解压缩很让人头大,每次都要查命令.转载下方便以后查阅.原文信息如下: 作者:capecape 来源:CSDN 原文:https://blog.csdn.net/capecape/a ...
- golang ---Learn Concurrency
https://github.com/golang/go/wiki/LearnConcurrency 实例1: package main import ( "fmt" " ...
- C# Winform控件字体大小自适应
using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace WGClie ...
- mvc_1_ex_stu_manage
Mvc第一遍结束综合练习:带权限的学生管理系统程序的设计应该根据功能来进行.先来设想一下本练习的程序功能:学生信息管理.登录用户区分权限.出错应该给出提示.由此可以设想,完成以后的程序是下图的样子:主 ...
- scxml 图像展示器 (基于C++ MFC GDI tinyxpath的实现)
以前的时候学习新东西没有总结的习惯,周末把以前研究的东西翻了翻,稍微总结下. Scxml是w3c出来的基于状态机的对话脚本语言标准,具体内容可以谷歌到,这里讲述自己开发的一个把scxml转化为可交互图 ...
- English--consonant_摩擦音
consonant_摩擦音_[f]和[v].[s]和[z].[∫]和[ʒ] 摩擦音:理论上可以无限延长气流. [f]:噘嘴,上牙咬住下唇,送气,通过气流摩擦发音,声带不震动.knife.food.le ...
- Matlab备忘录模式
备忘录模式(Memento)用于保存一个对象的某个状态,以便在适当的时候恢复对象.备忘录模式属于行为型模式,主要包括源发器,备忘录以及负责人.源发器:普通类,可以创建备忘录,也可以使用备忘录恢复状态. ...
- IntelliJ IDEA重命名变量的问题
当我尝试使用Shift+ F6或简单地使用Refactor => Rename重命名变量时,有时intellij不仅重命名我想要的那个,而且还重命名具有相同名称的所有其他变量(在其他文件中)以及 ...
- JavaScript 函数(一)
一.函数概述 1.概述 把一段相对独立的具有特定功能的代码块封装起来,形成一个独立实体,就是函数,起个名字(函数名),在后续开发中可以反复调用函数的作用就是封装一段代码,将来可以重复使用. 二.使用函 ...