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.

题目标签:Hash Table

  题目给了我们两个 array, 让我们找到在两个array 中重复的数字。

  利用HashSet,把nums1 的数字都存入set1;

  遍历nums2, 把nums2 与 set1 中重复的数字,存入HashSet intersect中;

  最后遍历intersect 把数字存入 res[] 返回。

Java Solution:

Runtime beats 71.41%

完成日期:06/05/2017

关键词:HashSet

关键点:利用两个HashSet

 class Solution
{
public int[] intersection(int[] nums1, int[] nums2)
{
HashSet<Integer> set1 = new HashSet<>();
HashSet<Integer> intersect = new HashSet<>();
int[] res;
int pos = 0; // store nums1 element into set1
for(int n: nums1)
set1.add(n); // store intersect element into intersect
for(int n: nums2)
{
if(set1.contains(n))
intersect.add(n);
} res = new int[intersect.size()]; for(Integer n: intersect)
res[pos++] = n; return res;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

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. 349 Intersection of Two Arrays 两个数组的交集

    给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示:    每个在结果中的元素必定是唯一的.    我们 ...

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

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

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

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

  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. LeetCode 349 Intersection of Two Arrays 解题报告

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

  7. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

  8. 15. leetcode 349. Intersection of Two Arrays

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

  9. [leetcode]349. Intersection of Two Arrays数组交集

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

随机推荐

  1. OC语言Block

    OC语言Block 一.Block (一)简介  Block是什么?苹果推荐的比较特殊的数据类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. Block和 ...

  2. PostgreSQL 备忘

    truncate table page_frame_mst; select setval('page_frame_mst_id_seq', 1, false): select setval('imag ...

  3. JS的filter用法

    filter也是一个常用的操作,它用于把Array的某些元素过滤掉,然后返回剩下的元素. 和map()类似,Array的filter()也接收一个函数.和map()不同的是,filter()把传入的函 ...

  4. SSH命令行传输文件到远程服务器

    Ubuntu操作系统 SCP命令 使用方式如下: 1.上传本地文件到远程服务器 scp /var/www/test.php root@192.168.0.101:/var/www/ 把本机/var/w ...

  5. 二叉查找树(Binary Search Tree)

    Date:2019-06-25 14:40:32 基本操作 注意:数据量较大时,插入建树的时间复杂度会很高,慎用! //查找 void Search(node *root, int x) { if(r ...

  6. xmpp聊天室(5)

    聊天室 //初始化聊天室 XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID]; xmppRoom = [[XMPPRoom alloc] initW ...

  7. ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

    例子: Post p = new Post(); p.setPostId(3); p.setPostName("技术"); 在执行数据保持时提示session.save(p); 的 ...

  8. ZOJ - 3992 - One-Dimensional Maze (思维)

    题意: 一条长度为n的直线,你一开始在位置m上 其中每个整点都有一个字符'L'或'R',如果是'L'那么你必须往左走一步,否则往右走一步 如果你到达位置1或位置n你任务就完成了 不过有可能你永远到不了 ...

  9. CCF201503-2 数字排序 java(100分)

    试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输 ...

  10. LINUX-文件的权限 - 使用 "+" 设置权限,使用 "-" 用于取消

    ls -lh 显示权限 ls /tmp | pr -T5 -W$COLUMNS 将终端划分成5栏显示 chmod ugo+rwx directory1 设置目录的所有人(u).群组(g)以及其他人(o ...