Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

题意:

如题

Solution1:  HashSet

1.  Scan nums1,  put items in nums1 to set1

2. Scan nums2, check each item is contained in set1

注意: HashSet.contains() -> O(1)

code

 class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet <Integer> set1 = new HashSet<>();
HashSet <Integer> resSet = new HashSet<>(); for (int i = 0; i < nums1.length; i ++) {
set1.add(nums1[i]);
}
for (int i = 0; i < nums2.length; i ++) {
if (set1.contains(nums2[i])) {
resSet.add(nums2[i]);
}
}
// resSet -> int[]res
int [] res = new int[resSet.size()];
int index = 0;
for (int num : resSet) {
res[index++] = num;
}
return res;
}
}

[leetcode]349. Intersection of Two Arrays数组交集的更多相关文章

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

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

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

  3. LeetCode 349. Intersection of Two Arrays (两个数组的相交)

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

  4. LeetCode 349 Intersection of Two Arrays 解题报告

    题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...

  5. LeetCode 349. Intersection of Two Arrays

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

  6. 15. leetcode 349. Intersection of Two Arrays

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

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

  8. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  9. Python 解LeetCode:Intersection of Two Arrays

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

随机推荐

  1. ubuntu 18.04下svn的安装与基本命令

    一.安装: apt-get install subversion 二.基本命令 1.将文件checkout到本地目录 svn checkout path(path是服务器 上的目录)例如:svn ch ...

  2. Web API源码剖析之HttpServer

    Web API源码剖析之HttpServer 上一节我们讲述全局配置.本节将讲述全局配置的DefaultServer,它是一个HttpServer类型. 主要作用就是接受每一次请求,然后分发给消息处理 ...

  3. 【Python编程:从入门到实践】chapter6 字典

    chapter6 字典 6.1 一个简单的字典 6.2 使用字典 6.2.1 访问字典中的值 6.2.2 添加键值对 6.2.3 先创建一个空字典 6.2.4 修改字典中的值 6.2.5 删除键值对 ...

  4. Xshell批量导入IP地址

    我的xshell被覆盖了~~~结果原来的host没了,很郁闷要一个一个添加,网上找了很长时间在Xshell中批量添加IP的文章,结果都不行. 最后找到了https://blog.netsarang.c ...

  5. spring boot 静态变量注入配置文件

    spring 静态变量注入 spring 中不支持直接进行静态变量值的注入,我们看一下代码: @Component(value = "KafkaConfig") @Configur ...

  6. uva-165-枚举

    题意:选取k种面额的邮票,总数是h,要求组合出来的连续数最大 枚举,网上看到一个更快的等价类划分,留着学等价类划分的思路 #include<stdio.h> #include<ios ...

  7. js选择器 querySelector

    <form method="post" action="" id="myform"> <input type=" ...

  8. 8.rem适配

    <!DOCTYPE html> <!--lang="en" : 英语 :声明当前页面的语言类型.--> <html lang="en&quo ...

  9. sqoop2的使用测试

    查看现有link sqoop:000> show link+-----------+------------------------+---------+|   Name    |     Co ...

  10. robot framework取出列表子元素

    取出嵌套列表变量的子元素 ${list}型列表: ${list} = [["A1", "first"], ["A2", "seco ...