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. TensorFlow OOM when allocating tensor with shape[5000,384707]

    在session范围内不要进行eval()或者convert_to_tensor()操作, 否则会造成OOM,或者报出错误:GraphDef cannot be larger than 2GB usi ...

  2. python生成动态个性二维码

    1 安装工具2 生成普通二维码3 带图片的二维码4 动态 GIF 二维码5 在Python程序中使用 一.安装 首先在python环境下运行, 打开cmd进入python27 进入scripts 然后 ...

  3. CAD使用DeleteXData删除数据(网页版)

    主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 js代码实现如下 ...

  4. csr_matrix参数解析

    压缩稀疏矩阵构造时的参数从官网看不明白,参考如下: >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.arr ...

  5. [gulp]Cannot find module 'orchestrator'

    从github 将项目 clone到本地后,运行gulp 启动项目时,出现这个问题的原因是: 1.clone 项目连同 nodemodules目录也一起下载到本地. 解决方式: 1.从本地删除node ...

  6. Linux:使用root踢掉其他用户

     首先使用w命令查看所有在线用户:  执行命令: pkill -kill -t tty3 再用w命令查看是否已经强制踢掉: TTY 是终端的意思    TTY :0 表示root用户登陆图形化界面的终 ...

  7. 【代码段】Android Studio使用DatePicker选择日期

    布局文件中放一个TextView就好了 Java文件如下: public class TestDatePickerActivity extends AppCompatActivity { privat ...

  8. Linux 复习一

    第一章 Linux简介和基本操作 一.Linux系统的来源和发展 Linux is a free Unix-type operating system originally created by Li ...

  9. #if 0的意义和好处

    在调试中经常遇到,写好的程序,需要调试相反的两方面,如:有两款单片机的程序,分别对应着不同的硬件引脚,我们把代码都写上了,但是不能同时让他们起效,通常的办法是/**/屏蔽一些段落,但是调试起来很麻烦. ...

  10. Java基础学习总结(88)——线程创建与终止、互斥、通信、本地变量

    线程创建与终止 线程创建 Thread类与 Runnable 接口的关系 public interface Runnable {         public abstract void run(); ...