LeetCode 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.
题解:
用一个HashSet 来保存nums1的每个element.
再iterate nums2, 若HashSet contains nums2[i], 把nums2[i]加到res中,并把nums[i]从HashSet中remove掉.
Time Complexity: O(nums1.length + nums2.length). Space: O(nums1.length).
AC Java:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> nums1Hs = new HashSet<Integer>();
for(int num : nums1){
nums1Hs.add(num);
}
List<Integer> res = new ArrayList<Integer>();
for(int num : nums2){
if(nums1Hs.contains(num)){
res.add(num);
nums1Hs.remove(num);
}
}
int [] resArr = new int[res.size()];
int i = 0;
for(int num : res){
resArr[i++] = num;
}
return resArr;
}
}
也可以使用两个HashSet.
Time Complexity: O(nums1.length + nums2.length). Space: O(nums1.length).
AC Java:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> nums1Hs = new HashSet<Integer>();
HashSet<Integer> intersectHs = new HashSet<Integer>();
for(int num : nums1){
nums1Hs.add(num);
}
for(int num : nums2){
if(nums1Hs.contains(num)){
intersectHs.add(num);
}
}
int [] res = new int[intersectHs.size()];
int i = 0;
for(int num : intersectHs){
res[i++] = num;
}
return res;
}
}
Sort nums1 and nums2, 再用双指针 从头iterate两个sorted array.
Time Complexity: O(nlogn). Space: O(1).
AC Java:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
HashSet<Integer> hs = new HashSet<Integer>();
int i = 0;
int j = 0;
while(i<nums1.length && j<nums2.length){
if(nums1[i] < nums2[j]){
i++;
}else if(nums1[i] > nums2[j]){
j++;
}else{
hs.add(nums1[i]);
i++;
j++;
}
}
int [] resArr = new int[hs.size()];
int k = 0;
for(int num : hs){
resArr[k++] = num;
}
return resArr;
}
}
sort nums1, 然后nums2 array 每一个element在 sorted 上做binary search.
Time Complexity: O(mlogm + nlogm), m = nums1.length, n = nums2.length.
Space: O(resArr.length).
AC Java:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> res = new HashSet<Integer>();
Arrays.sort(nums1);
for(int num : nums2){
if(binarySearch(nums1, num)){
res.add(num);
}
}
int [] resArr = new int[res.size()];
int i = 0;
for(int num : res){
resArr[i++] = num;
}
return resArr;
}
private boolean binarySearch(int [] nums, int target){
int low = 0;
int high = nums.length-1;
while(low <= high){
int mid = low + (high-low)/2;
if(nums[mid] < target){
low = mid+1;
}else if(nums[mid] > target){
high = mid-1;
}else{
return true;
}
}
return false;
}
}
跟上Intersection of Two Arrays II, Intersection of Three Sorted Arrays.
LeetCode Intersection of Two Arrays的更多相关文章
- [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 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: 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 ...
随机推荐
- 十一个行为模式之访问者模式(Visitor Pattern)
定义: 提供一个作用于某对象结构(通常是一个对象集合)的操作的接口,使得在添加新的操作或者在添加新的元素时,不需要修改原有系统,就可以对各个对象进行操作. 结构图: Visitor:抽象访问者类,对元 ...
- Docker化运维方式讲解
应用迁移需求 应用运维需要考虑的一个重要问题就是迁移, 在不同机器.机房.环境间迁移.迁移的原因有很多, 比如硬件过保(硬件故障), 机房迁移, 应用扩缩容等. 应用迁移的核心需求是: 简单.迁移操作 ...
- Storm的BaseBasicBolt源码解析ack机制
我们在学习ack机制的时候,我们知道Storm的Bolt有BaseBasicBolt和BaseRichBolt.在BaseBasicBolt中,BasicOutputCollector在emit数据的 ...
- 《IBM BPM实战指南》读书笔记
理论 BPM不是一个IT术语,更不是因技术的发展而起源的,相反,BPM自始至终都是管理学的术语和概念.它关注的一直都是效率.成本.利润.质量等核心问题.BPM是一门学科和一种方法论,只是现代的企业管理 ...
- 推荐8个实现 SVG 动画的 JavaScript 库
SVG 是一种分辨率无关的图形(矢量图形).这意味着它在任何类型的屏幕都不会遭受任何质量损失.除此之外,你可以让 SVG 灵活现一些动画效果.这篇文章就给大家推荐8个实现 SVG 动画的 JavaSc ...
- jQuery基础_4
dom对象就是jquery对象的数组组成部分jquery对象和dom对象的转化jquery对象-->dom对象$()[下标]dom对象-->jquery对象$(dom对象) jquery框 ...
- SharePoint 2013 Ajax 造成页面无法编辑
1.如下图,在编辑页面的时候,出现如下错误“此网页自上次打开后已被修改,必须再次打开该网页”,页面上没有什么特别的设置,就是default.aspx: 2.编辑之前页面,只有一个内容编辑器部件,和若干 ...
- Linux-HA实战(1)— Heartbeat安装
接触Heartbeat主要是因为之前项目中使用了TFS,最近想给nameserver做HA,因为TFS官方用的Heartbeat,所以刚好了解下,参考了网络上很多内容,这里简单记录下. 内容 环境和软 ...
- 让我们来谈谈JDBC
1.JDBC 1)JDBC简介 - JDBC就是Java中连接数据库方式 - 我们可以通过JDBC来执行SQL语句. 2)获取数据库连接 - j ...
- jQuery对表格的操作及其他应用
表格操作 1.隔行变色:对普通表格进行隔行换色:单击显示高亮样式:复选框选中高亮 <!DOCTYPE html> <html> <head> <meta ht ...