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, ...
随机推荐
- python行与缩进
#python行与缩进 1.了解逻辑行与物理行 2.行中分号使用规则 3.行链接 4.什么是缩进 5.如何缩进 6.注释 1.python中逻辑行主要是指一段代码,在意义上它的行数,而物理行,指的是我 ...
- java学习_5_21
数组的插入.删除.扩容本质上都是用的数组的复制.Java中数组的拷贝如下: System.arraycopy(Object src, int srcPos, Object dest, int dest ...
- vsCode scss安装
点击在settings.json中编辑写入代码: { /** Easy Sass 插件 **/ "easysass.formats": [ { "format" ...
- 王垠代表作《完全用Linux工作》
完全用Linux工作-王垠 <完全用Linux工作>作者:王垠 完全用 GNU/Linux 工作 理解 GNU/Linux 更多精彩请直接访问SkySeraph个人站点:www.sky ...
- xmpp 消息和好友上下线(3)
原始地址:XMPPFrameWork IOS 开发(四) 消息 //收到消息 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XM ...
- LeetCode_16 3SumCloest
3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...
- 洛谷——P1073 最优贸易
P1073 最优贸易 n 个城市间以 m 条有向道路连接, 小 T 从 1 号城市出发, 将要去往 n 号城市.小 T 观察到一款商品 Z 在不同的城市的价格可能不尽相同,小 T 想要在旅行中的某一个 ...
- docker 部署spring.boot项目【一】(引用外部配置文件)
上一篇随笔,nginx是启动运行在容器内,spring.boot的web项目是运行在宿主内,这一篇的目的,是把web项目也制作成镜像,然后在容器里启动. 文件目录结构如下: 主要文件结构说明:(1)b ...
- Python使用Flask框架,结合Highchart处理xml数据
1.html代码 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- vue中axios设置
//设置默认全局baseURL axios.defaults.baseURL=process.env.BASE_API; //设置默认全局携带浏览器cookie axios.defaults.with ...