题目:

给定两个数组,编写一个函数来计算它们的交集。

Given two arrays, write a function to compute their intersection.

示例 1:

输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2,2]

示例 2:

输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [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.

进阶:

  • 如果给定的数组已经排好序呢?你将如何优化你的算法?
  • 如果 nums1 的大小比 nums2 小很多,哪种方法更优?
  • 如果 nums2 的元素存储在磁盘上,磁盘内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?

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?

解题思路:

​ 暴力解题就不说了。

  • 哈希表:

    利用哈希映射求得其中一个数组每个数字出现的频次。遍历另一个数组,每遇到相同数字,其存储频次减一,若频次为 0,则移出哈希映射。如:

    输入 nums1 = [4, 9, 5], nums2 = [9, 4, 9, 8, 4 }
    计算 nums1 频次: { 4:1, 9:1, 5:1 },Key = {4 , 9, 5 }
    遍历 nums2:
    9 存在于 Key,9 频次 -1 为 0,9 移出 HashMap:{ 4:1, 5:1 }
    4 存在于 Key,4 频次 -1 为 0,4 移出 HashMap:{ 5:1 }
    9 不存在于 Key,跳过
    8 不存在于 Key,跳过
    ...
    输出:{9, 4}
  • 双指针:

    先把两个数组排序,定义两个指针 i, j ,移动指针

    • 如果 nums1 [i] == nums2 [j],则该数共有。
    • 如果不等,则移动元素值小的指针。

    如:

    输入 nums1 = [ 4, 9, 5 ], nums2 = [ 9, 4, 9, 8, 4 ]
    排序 nums1 = [ 4, 5, 9 ], nums2 = [ 4, 4, 8, 9, 9 ]
    双指针遍历:[ 4, 5, 9],[ 4, 4, 8, 9, 9 ]
    ^ ^
    4 = 4, 存入 res = [4], 移动双指针: [ 4, 5, 9],[ 4, 4, 8, 9, 9 ]
    ^ ^
    5 > 4, 移动指向 4 的指针:[ 4, 5, 9],[ 4, 4, 8, 9, 9 ]
    ^ ^
    5 < 8, 移动指向 5 的指针:[ 4, 5, 9],[ 4, 4, 8, 9, 9 ]
    ^ ^ 9 > 8, 移动指向 8 的指针:[ 4, 5, 9],[ 4, 4, 8, 9, 9 ]
    ^ ^ 9 = 9, 存入 res = [4, 9], 移动双指针: [ 4, 5, 9 ],[ 4, 4, 8, 9, 9 ]
    ^ ^
    超过 nums1 长度,结束,返回 res

回答进阶问题:

  1. 双指针法主要耗时操作就是排序操作的排序算法。对于有序数组当然使用双指针解题。
  2. 在一个数组长度远小于另一个数组时,哈希表解题更优,只需哈希统计长度较小的数组的元素频次,遍历长数组即可。时间复杂度取决于长数组长度。如果用双指针法,对长数组的排序将消耗更多时间。
  3. 如果内存有限,只能选择双指针法或暴力破解,无需额外空间复杂度。使用哈希表时在最坏情况下:每个数字只出现一次,HashMap 元素频次统计后,其大小可能大于内存容量。

哈希表解题:

Java:

class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();//动态数组
for (Integer num : nums1) map.put(num, map.getOrDefault(num, 0) + 1);//统计元素出现频次
for (Integer num : nums2) {//遍历另一个数组
if (map.containsKey(num)) {
int tmp = map.get(num)-1;//频次减一
if (tmp == 0) map.remove(num);//频次为 0 时,移出 HashMap
else map.put(num, tmp);//否则更新频次
list.add(num);//加入动态数组
}
}
//转为数组并返回
int size=list.size();
int[] res = new int[size];
for (int i = 0; i < size; i++) res[i] = list.get(i);
return res;
}
}

Python:

class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
count = dict(collections.Counter(nums1))# 自带库统计元素出现频次,并转为 Dict 类型 res = []
for num in nums2:# 遍历另一个数组
if num in count:
count[num] -= 1# 词频减一
res.append(num)
if count[num] <= 0:# 词频为 0 时,移出字典
count.pop(num)
return res

双指针解题:

Java:

class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> list = new ArrayList<>();
Arrays.sort(nums1);//排序数组
Arrays.sort(nums2);
int i = 0, j = 0;//定义指针
while (i < nums1.length && j < nums2.length) {
if (nums1[i] < nums2[j]) i++;//移动较小数的指针
else if (nums1[i] > nums2[j]) j++;//移动较小数的指针
else {
//两数相等则为交集,存入动态数组,移动双指针
list.add(nums1[i]);
i++;
j++;
}
}
//转为数组并返回
int[] res = new int[list.size()];
for (int k = 0; k < list.size(); k++) res[k] = list.get(k);
return res;
}
}

Python:

class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
i, j, nums1_size, nums2_size = 0, 0, len(nums1), len(nums2)# 定义指针,计算数组长度
nums1, nums2, res = sorted(nums1), sorted(nums2), []# 排序数组,初始化返回数组 res
while i < nums1_size and j < nums2_size:# 循环条件为指针不溢出
if nums1[i] < nums2[j]:# 移动数值较小的指针
i += 1
elif nums1[i] > nums2[j]:# 移动数值较小的指针
j += 1
else:
res.append(nums1[i])# 数值相等,则为交集,移动双指针
i += 1
j += 1
return res

欢迎关注微.信..公.众.号: 爱写Bug

LeetCode 350: 两个数组的交集 II Intersection of Two Arrays II的更多相关文章

  1. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  2. Java实现 LeetCode 350 两个数组的交集 II(二)

    350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入 ...

  3. Leetcode 350.两个数组的交集|| By Python

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...

  4. python(leetcode)-350两个数组的交集

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...

  5. [LeetCode] 350. 两个数组的交集 II intersection-of-two-arrays-ii(排序)

    思路: 先找到set的交集,然后分别计算交集中的每个元素在两个原始数组中出现的最小次数. class Solution(object): def intersect(self, nums1, nums ...

  6. 【Leetcode】【简单】【350. 两个数组的交集 II】【JavaScript】

    题目描述 350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2]输出: [2,2] 示例 2 ...

  7. LeetCode初级算法之数组:350 两个数组的交集 II

    两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...

  8. Java实现 LeetCode 349 两个数组的交集

    349. 两个数组的交集 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: num ...

  9. 领扣(LeetCode)两个数组的交集II 个人题解

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...

随机推荐

  1. grep的使用及正则表达式

    1.常用选项: -E :开启扩展(Extend)的正则表达式. -i :忽略大小写(ignore case). -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印. -n :显示行号 ...

  2. LeetCode刷题--两数相加(中等)

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  3. JavaScript-----13.内置对象 Math()和Date()

    1. 内置对象 js对象分为3种:自定义对象(var obj={}).内置对象.浏览器对象. 前两种对象是js基础内容,属于ECMAScript,第三个浏览器对象是js独有的.讲js API的时候会讲 ...

  4. 论文阅读:EGNet: Edge Guidance Network for Salient Object Detection

    论文地址:http://openaccess.thecvf.com/content_ICCV_2019/papers/Zhao_EGNet_Edge_Guidance_Network_for_Sali ...

  5. VirtualBox中重建Host-Only网卡后无法启动虚拟机

    问题: 在删除原有VirtualBox Host-Only虚拟网卡并重新添加后,虚拟机可能会无法启动,出现以下错误 Failed to open/create the internal network ...

  6. quarter软件的破解

    链接;http://www.openedv.com/forum.php?mod=viewthread&tid=275857&extra=page%3D1 这个是正点原子提供的破解方法, ...

  7. javascript数组在指定位置添加和删除元素

    在JavaScript中,Array对象提供了一个强大的splice()方法,利用这个方法可以达到在数组的指定位置添加和删除元素的目的. 指定位置删除元素 要在指定位置删除元素,可以使用splice( ...

  8. Mysql将日期转为字符串

    select date_format(time, '%Y-%m-%d %H:%i:%s') from info # 2019-08-22 21:03:21

  9. Linux CentOS上安装 MySQL 8.0.16

    前言: 因为我需要在我新安装的Linux CentOS系统服务器中安装和配置MySQL服务器,然而对于我们这种Linux使用小白而言在Linux系统中下载,解压,配置MySQL等一系列的操作还是有些耗 ...

  10. .NET轻松实现支付宝服务窗网页授权并获取用户相关信息

    前言: 最近在开发一个商业街区的聚合扫码支付功能,其中需要用到的有支付宝,微信两种支付方式,当然对于开发微信支付而已作为自己的老本行已经比较熟悉了,然而对于我来说支付宝支付还是头一次涉及到.这次项目中 ...