LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)
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 (两个数组的相交之二)的更多相关文章
- [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 ...
- [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 ...
- 350 Intersection of Two Arrays II 两个数组的交集 II
给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意: 输出结果中每个元素出现的次数, ...
- LeetCode 349. Intersection of Two Arrays (两个数组的相交)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- Python [Leetcode 350]Intersection of Two Arrays II
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
随机推荐
- jstree的基本使用例子
var menu = (function() { var _menu = {data:{}, initMenu : function() { $.jstree.defaults.core.themes ...
- HDU_1789_doing homework again_贪心
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 手机中快速看图,浏览编辑DWG 梦想极光CAD
梦想极光CAD6.0(2016.3.1) 手机版最新更新 1.增加手机上,图纸浏览时预览功能 2.增加直接从手机,QQ接收目录下加载文件功能 3.手机交互界面优化 4.增加新建图纸功能 5.增加缓存功 ...
- 牛客多校Round 5
Solved:3 rank:195 F. take 官方题解:小 A 在打开第 i 个箱子后会交换手中的钻石和第 i 个箱子中的钻石 当且仅当第 i个箱子的钻石是前 i 个箱子打开后出现的所有钻石里最 ...
- JS设计模式—节流模式的实际应用
在实际工作中,我们会经常遇到这样的业务场景,比如点击按钮提交表单,点击一次发一次请求,如果快速点击多次会发送多次请求,这样发送了多次请求是我们不愿意看到的.又比如输入框我们输入内容会调搜索的接口,那么 ...
- BeanFactory的生命周期
Bean自身的方法:调用Bean构造函数实例化Bean.调用setter设置Bean的属性值及通过<beam=n>的init-method和destory-method所制定的方法. Be ...
- P1541 乌龟棋 题解(洛谷,动态规划递推)
题目:P1541 乌龟棋 感谢大神的题解(他的写的特别好) 写一下我对他的代码的理解吧(哎,蒟蒻就这能这样...) 代码: #include<bits/stdc++.h> #define ...
- UVA - 12589 Learning Vector(dp-01背包)
题目: 思路: dp[j][h]表示选取了j个向量,且高度为h,利用01背包来解决问题. 没选当前的向量:dp[j][h] = dp[j][h]; 选了当前的向量:dp[j][h] = dp[j-1] ...
- linux od-输出文件的八进制、十六进制等格式编码的字节
博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 od命令用于输出文件的八进制.十六进制或其它格式编码的字节,通常用于显示或查看文件中不能直接显示在终端的字符. 常见的文件为文 ...
- 【Codeforces 484A】Bits
[链接] 我是链接,点我呀:) [题意] 让你求出l~r当中二进制表示1的个数最多的数x [题解] 最多有64位 我们可以从l开始一直增大到r 怎么增大? 找到l的二进制表示当中0所在的位置 假设i这 ...