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

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

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?

题目标签:Hash Table

  题目给了我们两个array, 让我们找到相交的数字,这一题与 #349 一样,只是可以包括重复的数字。

  所以利用HashMap 把 nums1 的数字和 出现次数存入;

  比较nums2 和 map1 把相交的数字 和 次数 存入 intersect;

  最后把intersect 里的 数字 按照它的出现次数 存入 int[] res。

Java Solution:

Runtime beats 23.67%

完成日期:06/05/2017

关键词:HashMap

关键点:利用两个HashMap

 class Solution
{
public int[] intersect(int[] nums1, int[] nums2)
{
HashMap<Integer, Integer> map1 = new HashMap<>();
HashMap<Integer, Integer> intersect = new HashMap<>();
int[] res;
int len = 0;
int pos = 0; // store nums1 numbers into map1
for(int n: nums1)
map1.put(n, map1.getOrDefault(n, 0) + 1); // compare nums2 with map1, store intersected numbers into intersect
for(int n: nums2)
{
if(map1.containsKey(n))
{
intersect.put(n, intersect.getOrDefault(n, 0) + 1);
len++; map1.put(n, map1.get(n) - 1); if(map1.get(n) == 0)
map1.remove(n);
} } res = new int[len]; for(int n: intersect.keySet())
{
for(int i=0; i<intersect.get(n); i++)
res[pos++] = n;
} return res;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

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

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

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

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

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

  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. [LintCode] Intersection of Two Arrays II 两个数组相交之二

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

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

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

  8. LeetCode 350. Intersection of Two Arrays II

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

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

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

随机推荐

  1. 微软MVC框架实战:开源的JS库Knockout

    [IT168 技术] Knowckout – 当MVC遭遇MVVM Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knock ...

  2. Angular——单页面实例

    基本介绍 1.引入的route模块可以对路由的变化做出响应 2.创建的控制器中依然需要$http向后台请求数据 3.php中二维数据的遍历用的是foreach 4.php中$arr=array(),$ ...

  3. oracle数据库过期

    本文转载自http://soft.chinabyte.com/database/6/12320006.shtml[来源:比特网 作者:悠虎] 由于Oracle11G的新特性所致,经常会遇到使用sqlp ...

  4. HDU_1874_畅通工程续_最短路问题

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. CAD控件:控件图形数据库概要说明

    1.1 控件数据库 3 1.1.1 数据库概述 3 1.2 数据库初始化 4 1.3 创建和组织数据库 4 1.4 保存数据库 4 1.5 插入一个数据库 4 1.6 设置当前数据库值 5 1.6.1 ...

  6. 梦想CAD控件网页版搜索图面上的文字

    在网页中查找到CAD控件图纸上的文字.点击此处在线演示. 主要用到函数说明: _DMxDrawX::NewSelectionSet 实例化一个构造选择集进行过滤,该类封装了选择集及其处理函数. _DM ...

  7. JSONP 应用

    受限于浏览器的同源安全策略, js 无法发起跨域的请求. 但是 HTML 中的 <script> 标签却可以引入跨域的文件使用. 而 JSONP 就是利用 <script> 的 ...

  8. case when里的like功能 ////// 截取(substr)

    case when里的like功能 假如要用到case when又要用到like这样的功能,即如果字符串包含‘语文’就怎么怎么样,包含‘数学’就怎么怎么样,包含‘英语’就怎么怎么样,like是用于wh ...

  9. LCS(HDU_5495 循环节)

    传送门:LCS 题意:给出两个序列an和bn,想在给出一个序列pn,问经过a[p1],,,,a[pn]和b[p1],,,b[pn]变换后序列a和序列b的最长公共子序列的长度是多少. 思路:对a[i]- ...

  10. npm 使用教程

    链接----------------------------------npm官网npm淘宝镜像 安装包----------------------------------npm install -g ...