【leetcode75】Intersection of Two Arrays(数组的交集)
题目描述:
给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次
例如:
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.
思路一:
1.使用HashMap(Integer,Boolean)数据结构,首先是便利Array1,放入map1
2.遍历Array2,判断map1是否包含,存入map2
3.取出map2的数据,存入数组输出
代码:
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
HashMap<Integer, Boolean> map1 = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> intersectMap = new HashMap<Integer, Boolean>();
for (int i = 0; i < nums1.length; i++) {
if (!map1.containsKey(nums1[i])) {
map1.put(nums1[i], true);
}
}
for (int j = 0; j < nums2.length; j++) {
if (map1.containsKey(nums2[j]) && !intersectMap.containsKey(nums2[j])) {
intersectMap.put(nums2[j], true);
}
}
int[] result = new int[intersectMap.size()];
int i = 0;
for (Integer e : intersectMap.keySet()) {
result[i] = e;
i++;
}
return result;
}
}
思路二:
- 先把两个数组排序
- 索引i,j分别代表Array1和Array2,相等都加1,谁小谁对应的索引加1
-
代码:
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0, j = 0;
int[] temp = new int[nums1.length];
int index = 0;
while (i < nums1.length && j < nums2.length) {
if (nums1[i] == nums2[j]) {
if (index == 0 || temp[index - 1] != nums1[i]) {
temp[index++] = nums1[i];
}
i++;
j++;
} else if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
int[] result = new int[index];
for (int k = 0; k < index; k++) {
result[k] = temp[k];
}
return result;
}
}
更多leetcode题目,请看我的leetcode专栏。链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode75】Intersection of Two Arrays(数组的交集)的更多相关文章
- [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笔记10 Intersection of Two Arrays(求交集)
问题描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [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] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [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 ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- centos 6安装 H3C iNode 上网客户端
我的安装目录是/usr/iNode 直接把客户端安装包拷到这个文件夹下然后解压: #rar x iNode2.-R0162.rar 然后进入文件夹,里边有一个install.sh文件,这是一个安装文件 ...
- UI相关
前端 UI 框架 https://github.com/twbs/bootstrap https://github.com/google/material-design-lite https://gi ...
- Oracle监听启动时报TNS-00507问题
Linux系统中,启动oracle监听的时候报如下错误: [oracle@centos ~]$ lsnrctl start LSNRCTL :: Copyright (c) , , Oracle. A ...
- 计算机网络之动态主机配置协议DHCP
为了将软件协议做成通用的和便于移植,协议软件的编写者不会把所有细节都固定在源代码中,而是把协议软件参数化,这就使得在很多台计算机上使用同一个经过编译的二进制代码成为可能. 一台计算机和另一台计算机的区 ...
- MPAndroidChart图形联动
MPAndroidChart图形联动 本篇基于博客MPAndroidChart的K线图上添加均线,两个MPAndroidChart是有联动效果的 原理 获取正在滑动的Chart的触摸事件,将事件同步给 ...
- Programming In Scala笔记-第八章、函数与闭包
当程序的代码量增大时,就需要对各功能模块进行分割,这些分割的小模块就是本文中接下来会进行分析的函数.接下来的部分会讲解包括函数嵌套,函数字面量,以及函数值等概念. 一.方法 一会函数一会方法的,是不是 ...
- Retrofit2.0+RxJava+Dragger2实现不一样的Android网络架构搭建
Tamic :csdn http://blog.csdn.net/sk719887916 众所周知,手机APP的核心就在于调用后台接口,展示相关信息,方便我们在手机上就能和外界交互.所以APP中网络框 ...
- EBS业务学习之库存管理
库存管理业务流程 企业结构 库存结构 库存结构定义 指定每个子库存的特性: •子库存的数量跟踪 •资产类子库存 •保留子库存 •净值子库存 •包含在有效承诺中Include in ATP •子库存级库 ...
- socket系列之什么是socket
1.什么是socket Socket是应用层与TCP/IP协议族通信的中间抽象层,它是一组接口,应用层通过调用这些接口实现发送和接收数据.一般这种抽象层由操作系统提供或者由JVM自己实现.使用sock ...
- Django 实现简单的文件上传
今天分享一下Django实现的简单的文件上传的小例子. 步骤 创建Django项目,创建Django应用 设计模型 处理urls.py 以及views.py 设计模板,设计表单 运行项目,查看数据库 ...