Intersection of Two Arrays(交集)
来源:https://leetcode.com/problems/intersection-of-two-arrays
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.
1. 直接使用HashSet
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Set<Integer> intersect = new HashSet<>();
for(int i=0; i<nums1.length; i++) {
set.add(nums1[i]);
}
for(int i=0; i<nums2.length; i++) {
if(set.contains(nums2[i])) {
intersect.add(nums2[i]);
}
}
int[] result = new int[intersect.size()];
int i = 0;
for(Integer num: intersect) {
result[i++] = num;
}
return result;
}
}// 6 ms
2. 类似BitMap的思想,LeetCode 1ms sample,缺点是数组中的元素不能为负数……
(1)求出两个数组的最大值
(2)建立 长度=最大值+1 的数组A,假设数组的索引为i,那么A[i]的值表示i是否在两个数组中存在,若为0表示不存在于任何一个数组中,为1表示存在于第一个数组中,为2表示两个数组中都存在
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int max = 0;
for(int num: nums1) {
if(num > max) {
max = num;
}
}
for(int num: nums2) {
if(num > max) {
max = num;
}
}
int[] indexMap = new int[max+1];
for(int num: nums1) {
indexMap[num] = 1;
}
int cnt = 0;
for(int num: nums2) {
if(indexMap[num] == 1) {
indexMap[num] = 2;
cnt++;
}
}
int[] result = new int[cnt];
for(int i=0; i<max+1; i++) {
if(indexMap[i] == 2) {
result[--cnt] = i;
}
}
return result;
}
}
Intersection of Two Arrays(交集)的更多相关文章
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
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 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
随机推荐
- WPF 多个选项卡TabControl 页面分离
此项目源码下载地址:https://github.com/lizhiqiang0204/TabControl-page-separation 每个页面的按键处理事件直接对应该页面下的cs文件 Main ...
- 小猴打架:prufer
我们概括题意,即将n个点组成一棵树的方案数. 所以是prufer. 又因为要考虑连边顺序,因此再乘上(n-1)!. 唔...难道你们都知道为什么是(n-1)!而不是n!吗... 反正我被这个地方卡了. ...
- Linux 性能测试工具Lmbench详解
Linux 性能测试工具Lmbench详解 2010-06-04 16:07 佚名 评测中心 字号:T | T Lmbench 是一套简易可移植的,符合ANSI/C 标准为UNIX/POSIX 而制定 ...
- 在CentOS 6.4上安装Puppet配置管理工具
在CentOS 6.4上安装Puppet配置管理工具 linux, puppetAdd comments 五052013 上篇说了下在ubuntu12.04上安装puppet,安装的版本为puppet ...
- 那些jquery或javascript花招
js定时器 定时器: 1)反复性定时器:格式:window.setInterval(“fn()”,1000); a)会反复执行 b)第二个参数是以毫秒计算的 2)一次性定时器:格式:window.se ...
- Linux入门培训教程 linux下拷贝cp删除rm移动mv命令参数以及说明
拷贝移动删除在windows中看起来这么简单,但linux经常使用的文字界面,所以对于linux系统 下拷贝cp删除 rm 移动mv命令参数就不得不需要了解和学习了 cp 该命令的功能是将给出的文件或 ...
- golang rabbitmq实践 (二 实现简单的消息收发)
1:驱动 本来打算自己写一个驱动的,后来发现github上面已经有了,那我就直接拿现成的了, 驱动采用 github.com/streadway/amqp ,直接import就可以啦! 2:excha ...
- A - 地震预测
A - 地震预测 怀特先生是一名研究地震的科学家,最近他发现如果知道某一段时间内的地壳震动能量采样的最小波动值之和,可以有效地预测大地震的发生. 假设已知一段时间的n次地壳震动能量的采样值为a1,a2 ...
- js中Array方法归类解析
为什么要对Array方法进行归类解析 因为它常用,而且面试必问 改变原数组的方法 pop 删除并返回数组最后一个元素push 从末尾给数组添加元素,返回新数组length值reverse 颠倒数组元素 ...
- ZXing使用详解与范例(C#)
介绍 ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码.(引自百度百科) 用 ...