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.

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?
 
 
这道题是之前那道 Intersection of Two Arrays 的拓展,不同之处在于这道题允许返回重复的数字,而且是尽可能多的返回,之前那道题是说有重复的数字只返回一个就行。那么这道题用 HashMap 来建立 nums1 中字符和其出现个数之间的映射, 然后遍历 nums2 数组,如果当前字符在 HashMap 中的个数大于0,则将此字符加入结果 res 中,然后 HashMap 的对应值自减1,参见代码如下:

解法一:

class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> m;
vector<int> res;
for (auto a : nums1) ++m[a];
for (auto a : nums2) {
if (m[a]-- > ) res.push_back(a);
}
return res;
}
};

再来看一种方法,这种方法先给两个数组排序,然后用两个指针分别指向两个数组的起始位置,如果两个指针指的数字相等,则存入结果中,两个指针均自增1,如果第一个指针指的数字大,则第二个指针自增1,反之亦然,参见代码如下:

解法二:

class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
int i = , j = ;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] == nums2[j]) {
res.push_back(nums1[i]);
++i; ++j;
} else if (nums1[i] > nums2[j]) {
++j;
} else {
++i;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/350

类似题目:

Intersection of Two Arrays

Find Common Characters

参考资料:

https://leetcode.com/problems/intersection-of-two-arrays-ii/

https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/82269/Short-Python-C%2B%2B

[LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二的更多相关文章

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

  2. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  3. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

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

  4. 350 Intersection of Two Arrays II 两个数组的交集 II

    给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意:       输出结果中每个元素出现的次数, ...

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

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

  6. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

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

  7. LeetCode 350. Intersection of Two Arrays II

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

  8. Python [Leetcode 350]Intersection of Two Arrays II

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  9. LeetCode 349. Intersection of Two Arrays (两个数组的相交)

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

随机推荐

  1. 『CSP2019初赛后的总结』

    初赛已经过去了,分数大概也已经知道了,接下来的一个月停课应该就是全部准备复赛. 联赛前几次讲课的内容是组合计数,计数\(dp\),字符串,概率期望,数论,数据结构,多数知识点难度都是大于联赛难度的,不 ...

  2. 【论文阅读】Objects as Points 又名 CenterNet | 目标检测

    目录 Abstract Instruction 分析 CenterNet 的Loss公式 第一部分:\(L_k\) 第二部分:\(L_{size}\) 第三部分:\(L_{off}\) Abstrac ...

  3. 动态ALV表实例-移动类型汇总

    TABLES:MSEG,MAKT. "定义结构 TYPES:BEGIN OF TY_DATA, MJAHR LIKE MSEG-MJAHR, "物料凭证的年份 MBLNR LIKE ...

  4. C# NPOI Excel

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. Python - File - 第十八天

    Python File(文件) 方法 open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OS ...

  6. Java学习——String,StringBuffer和StringBuilder

    Java学习——String,StringBuffer和StringBuilder 摘要:本文主要介绍了String字符串在内存中的存储情况,以及StringBuffer和StringBuilder的 ...

  7. AI 图像识别的测试

    随着AI 的浪潮发展,AI 的应用场景越来越广泛,其中计算机视觉更是运用到我们生活中的方方面面.作为一个测试人员,需要紧跟上 AI 的步伐,快速从传统业务测试,转型到 AI 的测试上来.而人脸识别作为 ...

  8. [日期工具分享][Shell]为特定命令依次传入顺序日期执行

    [日期工具分享][Shell]为特定命令依次传入顺序日期执行 使用方式: <本脚本文件名(必要时需要全路径)> <要执行的命令所在的文件名> <开始日期> < ...

  9. Django ajax 检测用户名是否已被注册

    添加一个 register.html 页面 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  10. excel 导出导入

    /** * 导出 * @param * @param * @return */ public function exportexcel() { set_time_limit(0); ini_set(' ...