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. s3express截图安装教程

    1.安装s3express_setup.exe 2.设置s3express 设置服务器地址setopt -endpoint:s3.cn-north-1.amazonaws.com.cn 设置协议set ...

  2. CentOS 6.4 i386 版本安装 FastDFS、使用Nginx作为文件访问WEB服务器

    安装环境:1. CentOS-6.4-i3862. FastDFS_v4.063. fastdfs-nginx-module_v1.154. Nginx-1.5.6(安装见此)5. libevent- ...

  3. Mysql replace into

    mysqlsql serverinsert 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以 ...

  4. VisualSVN:强制必须填写日志信息

    上回将到怎么修改已提交的版本日志信息,而开发项目过程中团队中总是有人忘记添加日志信息注释直接提交,这样会后期维护带来不便. 现在先演示一下效果 当直接提交一个空白日志信息时 有填写日志信息时 那怎么实 ...

  5. EXCEL 数组公式

    数组里的元素,同一行内的各元素用英文逗号“,”分开,用英文分号“;”将各行分开 3.单列数组与单行数组的计算 两个数组相加,查看结果是几行几列:在任意单元格输入公式“=A80:A83+B87:E87” ...

  6. python 之编写登陆接口

    基础需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 升级需求: 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时, ...

  7. ABAP-语音输出

    REPORT ZRICO_SPEAK. include ole2incl. data:ole type ole2_object, voice type ole2_object. parameters: ...

  8. 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值

    使用XMLHttpRequest对象分为4部完成: 1.创建XMLHttpRequest组建 2.设置回调函数 3.初始化XMLHttpRequest组建 4.发送请求 实例代码: [javascri ...

  9. 搭建Turbine时,报错误:Property or field 'default' cannot be found on object of type 'com.netflix.appinfo.InstanceInfo'

    Spring Boot + Eureka Server + Hystrix with Turbine: empty turbine.stream 配置的时候遇到了问题: Property or fie ...

  10. Windows7 64bit+python3.6环境下安装OpenCV3.3

    安装opencv3.3 打开windows的Python扩展包网址 根据自己的系统选择下载,这里我选择的是 通过pip3安装该whl文件,使用如下命令  pip3 install 该whl的绝对路径 ...