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. Python批量生成用户名

    写在最前 平时在工作中尤其是在做压测的时候难免需要一些用户名和密码,写个简单的Python小脚本批量生成一些 代码示例 import random,string #生成大小字母和数字一起的大字符串 a ...

  2. HTML——meta

    http://www.cnblogs.com/jr1993/p/4542862.html

  3. 北大ACM(POJ1018-Communication System)

    Question:http://poj.org/problem?id=1018 问题点:枚举. Memory: 564K Time: 329MS Language: C++ Result: Accep ...

  4. day17-常用模块II (hashlib、logging)

    目录 hashlib模块 撞库破解hash算法加密 logging模块 配置日志文件 hashlib模块 一般用于明文加密,其实就是一个自定义的字符编码表.原来0和1转换成字符,而现在的是字符转成另一 ...

  5. Deepin系统关于每次启动终端都要输入source /etc/profile的问题

    关于每次启动终端都要输入source /etc/profile的问题 当我在Deepin系统中下载了node以及npm之后,我为了将node导入到系统文件,使用了以下命令sudo gedit ``/e ...

  6. Python学习-字符串函数操作3

    字符串函数操作 isprintable():判断一个字符串中所有字符是否都是可打印字符的. 与isspace()函数很相似 如果字符串中的所有字符都是可打印的字符或字符串为空返回 True,否则返回 ...

  7. buf.readDoubleBE()

    buf.readDoubleBE(offset[, noAssert]) buf.readDoubleLE(offset[, noAssert]) offset {Number} 0 <= of ...

  8. LES on MCT

  9. 关于vuex自己理解的三幅图

  10. Spring security 5 Authorize Configuration

    1. Spring Security 核心请求,认证配置类 WebSecurityConfigurerAdapter protected void configure(HttpSecurity htt ...