[LeetCode] NO. 349 Intersection of Two Arrays
[题目]
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相关推荐
- leetcode 349. Intersection of Two Arrays 1
Description Given two arrays, write a function to compute their intersection. Example: Given nums1 = ...
- leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- 349. Intersection of Two Arrays 两个数组的交集
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2: 输入:nums1 = [4,9,5], ...
- LeetCode 350. Intersection of Two Arrays II
题目: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- 53 两数组的交集(Intersection of Two Arrays)
文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 1 题目 题目:两数组的交集( ...
- Leetcode: Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- LeetCode之Intersection of Two Arrays
1.题目 Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
最新文章
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]
- 第十一周项目实践2 用邻接表存储的图来实现基本应用
- spark安装测试过程中提示console:10: error: not found: value sc
- android 定制ui,AndroidSDK-UI定制
- java 2d 教程_Java 2D开发技巧之“灯光与阴影”
- 小程序 | 微信小程序多图上传(点击上传、可修改删除与放大回显)
- Find and Delete Files with Extension Name
- WLC-生成CSR操作
- C语言中使输入的字符串反序输出,C语言: 写一函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。...
- matlab在数学教学,Matlab在数学函数教学中的应用
- 宝藏机器学习资料分享(超高质量pdf直接下载)
- addr2line来定位问题
- 哪个云计算认证好?从市场角度浅析一下
- IDEA工具-鼠标滚轮调整字体大小
- 电子凸轮追剪曲线生成算法 算法,理解后可转成其他品牌PLC或任何一种编程语言
- kali启动后出现Resuming from hibernation错误
- 【洛谷】P5149 会议座位
- 什么是“沙箱”技术?
- hdu 1205 吃糖果(数学)
- 大文件分块计算MD5值 C++实现