原题链接在这里: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 IIIntersection of Three Sorted Arrays.

LeetCode Intersection of Two Arrays的更多相关文章

  1. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  2. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  3. LeetCode Intersection of Two Arrays II

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...

  4. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  5. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

  6. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  7. [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 ...

  8. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  9. [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 ...

随机推荐

  1. 配置管理工具 Puppet的安装和使用

    今天碰到一个linux下的puppet的问题,才发现原来这个是 用ruby语言编写的自动化的管理工具.有兴趣的同学,可以学习下. 这里重点讲述下 mac下 puppet的安装方法: 在Mac下采用Gi ...

  2. ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理

    话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...

  3. Tomcat下使用c3p0配置jndi数据源

    下载c3p0包: 下载地址:https://sourceforge.net/projects/c3p0/files/?source=navbar 解压后得到包:c3p0-0.9.2.jar,mchan ...

  4. 深入理解CSS盒子模型

    在CSS中浮动.定位和盒子模型,都是很核心的东西,其中盒子模型是CSS很重要基石之一,感觉还是很有必要把CSS盒子模型相关知识更新一下...... CSS盒子模型<BoxModel>示意图 ...

  5. SqlServer灾备方案(本地)

    如果你曾经有那么一个不经意的心跳来自于数据库数据损坏:错误的新增.更新.删除 .那么下面的方案一定能抚平你的创伤! 对于一个数据库小白来说,数据库的任何闪失带来的打击可说都是致命的.最初,我们让一个叫 ...

  6. javascript中的弹框

    大家都见过某度中的恶意广告,你关闭了又出来了!为何,JS来告诉你 效果猛戳此处 HTML <body> <h3 class="whiteColor">无法关 ...

  7. UML 用例图、顺序图、状态图、类图、包图、协作图、流程图

    ​用例图.顺序图.状态图.类图.包图.协作图 面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling La ...

  8. table tr foreach td 换行

    @{ ;} <table style=" class="sy_table"> <tr> @foreach (DataRow dr in (View ...

  9. jQuery插件中文乱码解决办法

    修改jQuery插件源代码的时候,中文字符有时候会显示乱码,通常这个插件是老外写的,默认文件的编码格式ANSI格式,源代码写中文字符的时候就会显示乱码,解决方法就是将文件编码格式换成UTF-8格式 保 ...

  10. 征友记之---前端开发Partner

                                                       简单自述下: 本人前端开发者,深圳一公司在职员工,非出自前端科班,乃自学前端已近两年之久. 受前端 ...