给定两个数组,写一个方法来计算它们的交集。
例如:
给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].
注意:
       输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
       我们可以不考虑输出结果的顺序。
跟进:
    如果给定的数组已经排好序呢?你将如何优化你的算法?
    如果 nums1 的大小比 nums2 小很多,哪种方法更优?
    如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存中,你该怎么办?
详见:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
C++:

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

参考:https://www.cnblogs.com/grandyang/p/5533305.html

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

  1. leecode刷题(6)-- 两个数组的交集II

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

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

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

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

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

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

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

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

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

  6. 6、两个数组的交集 II

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

  7. LeetCode 350: 两个数组的交集 II Intersection of Two Arrays II

    题目: 给定两个数组,编写一个函数来计算它们的交集. Given two arrays, write a function to compute their intersection. 示例 1: 输 ...

  8. 两个数组的交集 II [ LeetCode - 350 ]

      原题地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/description/   给定两个数组,写一个方法来计算 ...

  9. 350. 两个数组的交集 II

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

随机推荐

  1. Extract local angle of attack on wind turbine blades

    Extract local angle of attack on wind turbine blades Table of Contents 1. Extract local angle of att ...

  2. PAT 1131 Subway Map

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  3. TestNG常用注解

    原文链接:https://www.yiibai.com/testng/basic-annotations.html    以下是TestNG支持的注释列表: 注解 描述 @BeforeSuite 在该 ...

  4. 【Codeforces 923A】Primal Sport

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 考虑怎么得到数字x2=N,假设是质数p的倍数 那么x1肯定在x2-p+1~x2这个范围内才行 因为p的倍数要刚好大于等于x1, 所以x1肯定是 ...

  5. json转换时区问题-------前端展示时间少8小时--解决方法

    在application配置文件中加如下: spring.jackson.time-zone=GMT+8

  6. Java并发编程:线程池 - 实例

    代码块: public class test { public static void main(String[] args) { test t = new test(); ThreadPoolExe ...

  7. mongodb shell之使用js(二)

    mongodb shell之使用js(二) mongodb shell不仅是个交互式shell,还能够使用js脚本进行访问. 使用js脚本进行交互的优点与缺点 (1)无需任何驱动或语言支持: (2)方 ...

  8. poj 1562 简单深搜

    //搜八个方向即可 #include<stdio.h> #include<string.h> #define N 200 char ma[N][N]; int n,m,vis[ ...

  9. N的倍数

    题目来源: Ural 1302 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个长度为N的数组A,从A中选出若干个数,使得这些数的和是N的倍数. 例如:N = ...

  10. [bzoj4987]Tree_树形dp

    Tree bzoj-4987 题目大意:给定一颗n个点的有边权的树,选出k个点,使得:$\sum\limits_{i=1}^{k-1}dis_idis_j$最小. 注释:$1\le n\le 3000 ...