【leetcode76】Intersection of Two Arrays II
题目描述:
给定两个数组求他们的公共部分,输出形式是数组,相同的元素累计计数
例如:
nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
原文描述:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
Each element in the result must be unique.
The result can be in any order.
思路:
- 使用HashMap(Integer,Integer)的数据结构,首先遍历Array1
- 遍历Array2,如果Array1包含,而且get(key)的value减一还大于0,就继续
代码:
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < nums1.length; i++) {
if (!map.containsKey(nums1[i])) {
map.put(nums1[i], 1);
} else {
map.put(nums1[i], map.get(nums1[i]) + 1);
}
}
for (int j = 0; j < nums2.length; j++) {
if (map.containsKey(nums2[j]) && map.get(nums2[j]) > 0) {
map.put(nums2[j], map.get(nums2[j]) - 1);
if (!resultMap.containsKey(nums2[j])) {
resultMap.put(nums2[j], 1);
} else {
resultMap.put(nums2[j], resultMap.get(nums2[j]) + 1);
}
}
}
int sum = 0;
for (Integer e : resultMap.keySet()) {
int count = resultMap.get(e);
sum += count;
for (int i = 0; i < count; i++) {
list.add(e);
}
}
int[] result = new int[sum];
for (int i = 0; i < sum; i++) {
result[i] = (int) list.get(i);
}
return result;
更多leetcode题目,请看我的leetcode专栏。链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode76】Intersection of Two Arrays II的更多相关文章
- 【leetcode75】Intersection of Two Arrays(数组的交集)
题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- LeetCode_350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...
随机推荐
- JDBC:从原理到应用
一.是为何物 1.概念 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...
- SQL SERVER Management Studio
1. 实验目的 熟悉SQL SERVER Management Studio的部分操作 数据SQL SERVER简化版和完整版数据库设计 2. 实验内容 2.1. 熟悉简化版SQL ...
- day08 JSP
day08 JSP 1. jsp 入门和 jsp 运行原理 2. jsp 语法 2.1 jsp 模板元素:jsp 页面中的 html 内容.它定义了网络基本骨架,即定义了页面结构和外观. 2.2 js ...
- 分布式一致性协议Raft原理与实例
分布式一致性协议Raft原理与实例 1.Raft协议 1.1 Raft简介 Raft是由Stanford提出的一种更易理解的一致性算法,意在取代目前广为使用的Paxos算法.目前,在各种主流语言中都有 ...
- 《Shazam It! Music Recognition Algorithms, Fingerprinting, and Processing》译文
最近看到一篇老外写的博客,简单介绍了shazam的工作原理.图非常好,所以就把它翻译成中文,希望对搞听歌识曲的人有帮助. 你可能遇到这样的场景:在酒吧或者餐厅听到你非常熟悉的歌,也许你曾经听过无数次, ...
- Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Linux 环境下一些常用的命令(二)
11. chown命令 "chown"命令就是改变文件拥有者和所在用户组.每个文件都属于一个用户组和一个用户.在你的目录下,使用"ls -l",你就会看到像这样 ...
- 02_3中方式的反射,通过Class.forName获得Class对象,通过类.class获得字节码对象,通过类实例.getClass()的方式获得Class对象
反射中加载类: Java中有一个Class类用于代表某一个类的字节码 .class文件 对应Class //1 加载类 // java中Class代表一个类,但是到底代表哪个类要明确指出 ...
- 详解EBS接口开发之销售订单导入
步骤 1. 创建一个订单导入来源. - 导航到 OM -> 设置 -> 订单 -> 导入来源 - 输入一个新的订单导入来源名称和描述 - 选择启用来激活 ...
- Android Device Administration 设备管理器——实现一键锁屏
Android Device Administration 设备管理器--实现一键锁屏 最近研究了一下安全这一块的内容,当然,我是比较水的,所以也拿不出什么好知识点,但是有一些冷门的东西我还是可以聊聊 ...