原题链接在这里: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. 使用git error: RPC failed; result=22, HTTP code = 411

    使用git提交比较大的文件的时候可能会出现这个错误 error: RPC failed; result=22, HTTP code = 411 fatal: The remote end hung u ...

  2. 分享20个最新的免费 UI 设计素材给设计师

    用户界面设计涉及到很多的创意,灵感以及需要与客户进行有效沟通的技能.良好的用户界面是一致的,可以使网站更容易理解和使用.UI设计的重点在于用户体验和互动,同时易于使用对于一个成功的移动应用程序来说非常 ...

  3. JavaScript基本语法(三)

    上篇博文说到JS的运算符,这次说说JS程序流程控制. 1. 条件语句 if 语法: if(condition) statements1 else statement2 当括号里的条件成立的时候,执行i ...

  4. sharePoint 2016 弃用和删除的功能

    前言 sharepoint 2016版本正式发布,但是相比较2013版本,还是删除或者准备删除一些功能,我们需要了解一下哪些功能已经被删除掉或者在下一个版本中移除,因为这些可能影响我们现有系统是否能够 ...

  5. IBatis.Net项目数据库SqlServer迁移至Oracle经验

    最近完成了一个(IBatis.Net+MVC)项目的数据库+代码迁移工作,可把我折腾得~~~ IBatis.Net是一个ORM框架,具体介绍可以问度娘.我之前没用ORM框架使用经验,所以这一路我不是走 ...

  6. iOS Swift-HelloWord

    iOS Swift-HelloWord 按部就班选择Swif开发语言,输出HelloWord. override func viewDidLoad() { super.viewDidLoad() pr ...

  7. Android自定义控件(二)

    这一篇主要来讲一下自定义控件中的自定义viewgroup,我们以项目中最常用的下拉刷新和加载更多组件为例 简单介绍一下自定义viewgroup时应该怎么做. 分析:下拉刷新和加载更多的原理和步骤 自定 ...

  8. 基于物理渲染的渲染器Tiberius计划

    既然决定实现一个光栅化软件渲染器,我又萌生了一个念头:实现一个基于物理渲染的渲染器.

  9. ORACLE基本数据类型总结

    ORACLE基本数据类型(亦叫内置数据类型 built-in datatypes)可以按类型分为:字符串类型.数字类型.日期类型.LOB类型.LONG RAW& RAW类型.ROWID &am ...

  10. ORACLE会话连接进程三者总结

    概念介绍 通俗来讲,会话(Session) 是通信双方从开始通信到通信结束期间的一个上下文(Context).这个上下文是一段位于服务器端的内存:记录了本次连接的客户端机器.通过哪个应用程序.哪个用户 ...