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的更多相关文章

  1. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  2. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  3. 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 ...

  4. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

  5. [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 ...

  6. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  7. [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 ...

  8. leetcode349 350 Intersection of Two Arrays & II

    """ Intersection of Two Arrays Given two arrays, write a function to compute their in ...

  9. LeetCode 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

随机推荐

  1. 洛谷P3957:跳房子——题解

    https://www.luogu.org/problem/P3957 沉迷普及组题无法自拔. 显然二分答案,然后里面套个dp,$f[i]$表示跳到第$i$个格子的最大得分,复杂度$O(n^2logn ...

  2. c语言数据结构之线性表的顺序存储结构

    线性表,即线性存储结构,将具有“一对一”关系的数据“线性”地存储到物理空间中,这种存储结构就称为线性存储结构,简称线性表. 注意:使用线性表存储的数据,要求数据类型必须一致,线性表存储的数据,要么全不 ...

  3. Django 路由正则URL

    Django 路由正则URL URL1 # 路由 url(r'^detail/', views.detail) {#点击跳转到指定用户下显示信息#} <li><a target=&q ...

  4. jQuery中使用Ajax获取JSON格式数据示例代码

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...

  5. [個人紀錄] windows form , usercontrol design 模式不見

    windows form 跟 usercontrol 都變成cs檔 無法點擊進入設計模式 <Compile Include="Form1.cs"/> <Compi ...

  6. SQL Server中查找包含某个文本的存储过程 SQL 查找存储过程中出现过的文字怎么查询 查询整个数据库中出现的文本 sql 全局搜索

    --将text替换成你要查找的内容SELECT name, *FROM sysobjects o, syscomments sWHERE o.id = s.id AND text LIKE '%tex ...

  7. ubuntu Ifconfig只显示一个lo

    第一步启动网卡 是网卡未启动 命令: ifconfig -a,显示所有网络接口的信息. ifconfig显示当前激活的网络接口信息 ifconfig eth0 up 启动网卡 ifconfig -a ...

  8. 去世父亲在儿子手机中复活,这可能是最温暖的一个AI

    美国青年James Vlahos的父亲不幸因病去世,但聊以慰藉的是,现在他每天还能和父亲聊天并收到回复,而且父亲在回复中的口吻与语气,就仿佛还「活着」一样. 这并不是恐怖片剧情,而是科技的魔幻力量:回 ...

  9. html5单词

    < meta   charset = " UTF-8 " >         国内编码     (meta-标签用来描述一个HTML网页文档的属性  charset-字 ...

  10. spark任务分配----TaskSchedulerImpl源码解析

    TaskSchedulerImpl 上一篇讲到DAGScheduler根据shuffle依赖对作业的整个计算链划分成多个stage之后,就开始提交最后一个ResultStage,而由于stage之间的 ...