【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这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- python笔记八(切片)
一.切片 首先我们要记得在Python中可以用于切片的对象有 列表.元组.字符串. 切片操作就是直接从列表.元组或字符串中,选择出我们想要的内容,这些操作非常简洁实用. >>> L ...
- Python3 错误和异常
Python有两种错误很容易辨认:语法错误和异常. 语法错误 Python 的语法错误或者称之为解析错,是初学者经常碰到的,如下实例 >>> while True print('He ...
- Jmeter(六)_前置处理器
BeanShell PreProcessor 使用BeanShell在请求进行之前进行操作.语法使用与BeanShell Sampler是一样的.但可使用的内置变量稍有不同 参考示例 Jmeter ...
- JavaScript的事件、DOM模型、事件流模型以及内置对象详解(三)
JS中的事件 JS中的事件分类 1.鼠标事件: click/dbclick/mouseover/mouseout 2.HTML事件: onload/onunload/onsubmit/onresize ...
- Android需求之点击跳转至市场评价
相信大家都看过APP上有一个选项"喜欢此APP?还希望您评价一下吧!",然后点击弹出选择框让你选择一个市场如: 安智市场,百度应用,豌豆荚-.然后选择其中一个后就跳转至此市场你的A ...
- Python 2.7 闭包的局限
想法源自:http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to- ...
- python命令行参数解析模块argparse和docopt
http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...
- Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的
Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...
- PHP+MySQL 分页那点事
分页技术随处可见,这可以算得上是最为基础的网站功能了.于是今天尝试着用PHP来实现一个分页的小例子. 准备工作 环境准备 Apache MySQL PHP 工作环境 数据库准备 建库 建表 预存数据 ...
- 高通开发笔记---yukon worknote
点击打开链接 daily buildhttp://android-ci-platform.cnbj.sonyericsson.net/job/daily_build_jb-mr2-yukon/DL-C ...