leetcode 349. Intersection of Two Arrays-[Java]
来源:互联网 发布:windows phone 10微信 编辑:程序博客网 时间:2024/05/22 15:47
原题链接:Intersection of Two Arrays
思路:利用set去除重复,查找两个集合中都有的元素添加进结果集;
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { /* Time complexity:O(m*n) Space Complexity:O(m+n) */ if(nums1.length==0 || nums2.length==0)return new int[0]; HashSet<Integer> iset1=new HashSet<>(); HashSet<Integer> iset2=new HashSet<>(); for(int i:nums1)iset1.add(i); for(int i:nums2)iset2.add(i); ArrayList<Integer>res=new ArrayList<>(); for(int i:iset1)if(iset2.contains(i))res.add(i); int[] ret=new int[res.size()]; for(int i=0;i<ret.length;i++)ret[i]=res.get(i); return ret; }}
阅读全文
0 0
- leetcode 349. Intersection of Two Arrays-[Java]
- [LeetCode] Intersection of Two Arrays(java)
- [LeetCode]Intersection of Two Arrays II(Java)
- [LeetCode-349] Intersection of Two Arrays(java)
- Leetcode 349. Intersection of Two Arrays 解题报告 Python Java
- leetcode 记录 349. Intersection of Two Arrays
- 349.[Leetcode]Intersection of Two Arrays
- [leetcode] 349. Intersection of Two Arrays
- Leetcode Everyday: 349. Intersection of Two Arrays
- leetcode.349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- 【leetcode】 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Python6:oriented objective programming
- 使用反射调用一个类的方法
- information_schema中Innodb相关表用于分析sql查询锁的使用情况介绍
- 用Tensorflow实现CNN文本分类(详细解释及TextCNN代码解释)
- 影响相机图像质量的因素-镜头
- leetcode 349. Intersection of Two Arrays-[Java]
- 将properties转换成json格式
- bzoj4515 [Sdoi2016]游戏
- jQuery
- hdu 1863 畅通工程 (prim与并查集)
- Android studio 实现多分包(多个dex文件)技术详解
- 牛客网JavaScript在线编程题语言选择问题
- WEB前端-HF-HTML5Programming-笔记-CH3 事件与处理程序
- 线程安全的AtomicLong使用