349. Intersection of Two Arrays
来源:互联网 发布:百度seo站长工具 编辑:程序博客网 时间:2024/06/06 17:45
问题描述:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].寻找两个数组的交集并且是只出现一次的,思路是利用JAVA中set的不可以包括两个重复元素的特性,第一次先将nums1放到一个一个set中,第二次再来寻找nums2中的元素,如果在set中,就将其放到最后的结果中,具体代码如下:
public int[] intersection(int[] nums1, int[] nums2) { HashSet<Integer>set=new HashSet<Integer>(); HashSet<Integer>end=new HashSet<>(); for (int i = 0; i < nums1.length; i++) { set.add(nums1[i]); } for (int i = 0; i < nums2.length; i++) { if (set.contains(nums2[i])) { end.add(nums2[i]); } } int[] res=new int[end.size()]; int index=0; for (int num:end) { res[index++]=num; } return res; }
0 0
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays[][
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- 349. Intersection of Two Arrays
- Dubbo ---- 启动时检查-直连提供者
- Devstack部署多节点Openstack
- 请输入密码
- maven简单的使用
- java学习笔记4.11常用类
- 349. Intersection of Two Arrays
- RxJava操作符(一) __创建操作
- spring+maven+mybatis+后台学习的坑点
- 521 Longest Uncommon Subsequence I
- 腾讯前端十日训练营day2
- 数据结构——二叉查找树
- 本地环境phpStorm10+XDebug配置和断点调试
- 判定Java源文件名称(Java)acm.sdut
- recycleView使用之2 :横向现实图片之图片是正方形