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 ...
随机推荐
- Notepad++技巧
1 正则表达式的查找和替换,例如删除每行开始的数字ctrl+H, ^\d\d\d:null 2 删除所有的空行: TextFX插件->Edit->Delete Blank Lines 3 ...
- 【Spring】Spring-Session-Data-Redis实现session共享
前言 在开发中遇到一个关于用户体验的问题,每次当运维进行更新重启服务器时,都会导致会员平台中已登录的用户掉线.这是因为每个用户的会话信息及状态都是由session来保存的,而session对象是由服务 ...
- GET和POST请求的区别和使用场景
本质上的区别: GET请求.处理.响应过程中只是产生一个TCP数据包,而POST请求会产生两个TCP数据包. 更具体地说,GET请求过程中头和请求正文数据一起到服务器端, 而POST请求过程中, ...
- keras学习笔记-bili莫烦
一.keras的backend设置 有两种方式: 1.修改JSON配置文件 修改~/.keras/keras.json文件内容为: { "iamge_dim_ordering":& ...
- iview2.0 日期选择器DatePicker 所选时间格式不对
网上有很多解决方式,大部分都是加个@on-change事件.比如下图: 但是如果是编辑的时候,打开编辑页面,通过数据库返回时间显示出来是对的,但是不触发change事件,直接点保存的话,保存后还是少8 ...
- JPA学习(一、JPA_Hello World)
框架学习之JPA(一) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...
- clojure的delay future promise
<Clojure编程>第4章笔记. 总的感觉,Clojure毕竟是基于JVM的在人间的工程化语言.不是纯的无状态纯函数的在神间的lisp. 作为后端语言,不可避免要处理计算中和代码执行时序 ...
- luogu P1223 排队接水 x
P1223 排队接水 题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小. 输入输出格式 输入格式: 输入文件共两行, ...
- Netty 介绍和应用场景(一)
1.为什么选择Netty 需要了解了Socket通信(IO/NIO/AIO)编程,对于通信模型已经有了一个基本的认识.,果想把这些真正的用于实际工作中,那么还需要不断的完善.扩展和优化.比如经典的TC ...
- Cisco基础(三):HSRP配置、三层交换配置HSRP、STP的配置、三层交换配置STP
一.HSRP配置 目标: 在企业网络到外部的连接方案中,要求不高的条件下可以是单出口.一旦该出口线路出现问题,整个企业网络就不能连接到外网了.为了使得企业网络到外网连接的高可用性,可以设置两个以上的出 ...