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. 拒绝了对对象 'data'(数据库 'xxx',所有者 'dbo')的 SELECT 权限

    拒绝了对对象 'data'(数据库 'xxx',所有者 'dbo')的 SELECT 权限解决的方法:1>在数据库里相应的用户权限中,把db_denydatareader的复选框的勾去掉.很多初 ...

  2. Concurrent control in SQLite

    This document describes the technologies to concurrent access to a SQLite database. There are also s ...

  3. 使用python获得N个区分度较高的RGB颜色值

    获得任意N个区分度最高的RGB颜色值是一个经典的问题,之前在做一些可视化的东西时需要解决这个问题.首先去网上找了一些方法,未果,于是想自己来搞,心里的想法是,先给出一个距离函数用来度量两个RGB颜色值 ...

  4. 05Microsoft SQL Server 表创建,查看,修改及删除

    Microsoft SQL Server 表创建,查看,修改及删除 创建表 创建普通表 use 数据库名称 go create table 表名称( 列1 ) not null, 列2 ) not n ...

  5. CodeFrist基础_Fluent Api

    一丶首先新建两个实体类 public class Student { public int StudentKey { get; set; } public string StudentName { g ...

  6. Vue组件传值方法调用

    1.子组件改变父组件的值 <father  label="云盘快照" name="name2"> <son :props='rows'   @ ...

  7. 文艺平衡树-splay的区间操作

    真的是个神题,蒟蒻表示无力吐槽.刚开始以为是一个板子题,看着题解打了一遍,大概也理解了他是怎么实现的,然后我就去做别的题了,然后就在Three_D大佬的询问下蒙*了.最后还是问的nc哥,并思考了一个中 ...

  8. videojs

    <link href="http://vjs.zencdn.net/5.5.3/video-js.css" rel="stylesheet"> &l ...

  9. Java基本数据类型、包装类与String类之间的转换

    一.基本数据类型与包装类之间的转换: import org.junit.Test; public class MainTest { /** * 基本数据类型与包装类之间的转换 */ @Test pub ...

  10. C++ Primer(第4版)-学习笔记-第5部分:高级主题

    第17章  用于大型程序的工具 异常处理 不存在数组或函数类型的异常.相反,如果抛出一个数组,被抛出的对象转换为指向数组首元素的指针,类似地,如果抛出一个函数,函数被转换为指向该函数的指针. 不要抛出 ...