【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 ... 
随机推荐
- Java网络爬虫Hello world实现——Httpclient爬取百度首页
			1.创建Maven项目 2.Httpclient Maven地址 <dependency> <groupId>org.apache.httpcomponents</gro ... 
- REACT相关资料合集
			===实例=== https://github.com/jesseskinner/react-webpack-demo ===UI组件库=== https://github.com/amazeui/a ... 
- vim 基本命令入门
			简介 vim是Linux 系统下类似于Windows的记事本的编辑器. vim 中经常使用的三种模式 一般模式:浏览文件内容. 插入模式:编辑文件内容. 底行模式:进行保存编辑内容,退出等操作. 基本 ... 
- Android 5.0新控件——FloatingActionButton(悬浮按钮)
			Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ... 
- Python实现Mysql数据库连接池
			python连接Mysql数据库: python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访 ... 
- JavaScript DOM详解
			欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52727448 本文出自:[余志强的博客] 一.DOM概述 D: Do ... 
- springMVC源码分析--SimpleServletHandlerAdapter(二)
			上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ... 
- 【伯乐在线】最值得阅读学习的 10 个 C 语言开源项目代码
			原文出处: 平凡之路的博客 欢迎分享原创到伯乐头条 伯乐在线注:『阅读优秀代码是提高开发人员修为的一种捷径』http://t.cn/S4RGEz .之前@伯乐头条 曾发过一条微博:『C 语言进阶有 ... 
- 关于Lt分发系统的时序图分析
			我们已经知道,系统共分为两个模块,mather与son 同时系统允许的操作也有三种,向mather提交war包,我某个服务器更新代码,为所有服务器更新代码 我们一个一个来看 先说,向mather提交w ... 
- UNIX网络编程——epoll 的accept , read, write(重要)
			在一个非阻塞的socket上调用read/write函数,返回EAGAIN或者EWOULDBLOCK(注:EAGAIN就是EWOULDBLOCK). 从字面上看,意思是: EAGAIN: 再试一次 E ... 
