[题目]

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

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

[题目解析] 这是一个很简单的求交集的问题,可以用Set来解决。如下。

    public static int[] intersection(int[] nums1, int[] nums2){        Set<Integer> set = new HashSet<Integer>();Set<Integer> interset = new HashSet<Integer>();for(int num : nums1){set.add(num);}for(int num : nums2){if(set.contains(num)){interset.add(num);}}int result[] = new int[interset.size()];int j = 0;for(Integer num : interset){result[j++] = num;}return result;}

转载于:https://www.cnblogs.com/zzchit/p/5770662.html

[LeetCode] NO. 349 Intersection of Two Arrays相关推荐

  1. leetcode 349. Intersection of Two Arrays 1

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

  2. leetcode 349. Intersection of Two Arrays

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

  3. 349. Intersection of Two Arrays 两个数组的交集

    给定两个数组,编写一个函数来计算它们的交集.   示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2: 输入:nums1 = [4,9,5], ...

  4. LeetCode 350. Intersection of Two Arrays II

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

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

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

  6. 53 两数组的交集(Intersection of Two Arrays)

    文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 1 题目 题目:两数组的交集( ...

  7. Leetcode: Intersection of Two Arrays

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

  8. LeetCode Intersection of Two Arrays

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

  9. LeetCode之Intersection of Two Arrays

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

  10. C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...

最新文章

  1. android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]
  2. 第十一周项目实践2 用邻接表存储的图来实现基本应用
  3. spark安装测试过程中提示console:10: error: not found: value sc
  4. android 定制ui,AndroidSDK-UI定制
  5. java 2d 教程_Java 2D开发技巧之“灯光与阴影”
  6. 小程序 | 微信小程序多图上传(点击上传、可修改删除与放大回显)
  7. Find and Delete Files with Extension Name
  8. WLC-生成CSR操作
  9. C语言中使输入的字符串反序输出,C语言: 写一函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。...
  10. matlab在数学教学,Matlab在数学函数教学中的应用
  11. 宝藏机器学习资料分享(超高质量pdf直接下载)
  12. addr2line来定位问题
  13. 哪个云计算认证好?从市场角度浅析一下
  14. IDEA工具-鼠标滚轮调整字体大小
  15. 电子凸轮追剪曲线生成算法 算法,理解后可转成其他品牌PLC或任何一种编程语言
  16. kali启动后出现Resuming from hibernation错误
  17. 【洛谷】P5149 会议座位
  18. 什么是“沙箱”技术?
  19. hdu 1205 吃糖果(数学)
  20. 大文件分块计算MD5值 C++实现

热门文章

  1. 中国人上淘宝喜欢干啥 你知道吗?
  2. 如何在WP7中实时监控内存使用量
  3. td.moveRow方法
  4. 分享JTooltips 0.2beta版Demo
  5. ERP必须基于企业流程管理
  6. Toontrack Superior Drummer for Mac - 鼓音乐制作工具
  7. webApp调试页面的几种方式
  8. 一首歌是怎么诞生的?
  9. centos7系统搭建DNS服务器
  10. C# 使用 Windows 消息队列机制