当前位置:网站首页>349、两个数组的交集
349、两个数组的交集
2022-04-23 10:11:00 【Popuessing's Jersey】
题意:给定两个数组,编写一个函数来计算它们的交集
解决问题之前的一些小知识:
HashSet是实现Set接口,也就是Set的实现类,由哈希表支持的(实现一个HashMap实例)
源码:
private transient HashMap<E,Object> map;
//默认构造器
public HashSet() {
map = new HashMap<>();
}
//将传入的集合添加到HashSet的构造器
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
//明确初始容量和装载因子的构造器
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
//仅明确初始容量的构造器(装载因子默认0.75)
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
public class Lianggeshuzudejiaji {
public int[] intersection(int[]nums1 ,int[] nums2){
//如果有其中一个数组为空,返回空集合
if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length==0){
return new int[0];
}
//创建集合存放元素
Set<Integer> set = new HashSet<>();
Set<Integer> reSet = new HashSet<>();
//遍历数组1
for (int i: nums1) {
set.add(i);
}
//遍历数组2的过程中判断哈希表中是否存在该数组
for (int i: nums2) {
//如果出现数组1中存在的元素,将重复元素放入新的集合中
if (set.contains(i)){
reSet.add(i);
}
}
//根据数组2判断重复后的集合大小,创建一个新的数组
int [] resArr = new int[reSet.size()];
int index = 0;
//取出集合中的元素,将结果转为数组
for (int i : reSet) {
resArr[index++] = i;
}
return resArr;
}
public static void main(String[] args) {
int [] nums1 = {4,2,2,1};
int [] nums2 = {2,3,4};
Lianggeshuzudejiaji lianggeshuzudejiaji =new Lianggeshuzudejiaji();
int [] res = lianggeshuzudejiaji.intersection(nums1,nums2);
for (int x:res) {
System.out.print(x+" ");
}
}
输出结果:
2 4
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121475581
边栏推荐
- Realize data value through streaming data integration (2)
- 《谷雨系列》空投
- Realizing data value through streaming data integration (5) - stream processing
- ARM调试(1):两种在keil中实现printf重定向到串口的方法
- Realize data value through streaming data integration (3) - real-time continuous data collection
- Nvidia最新三维重建技术Instant-ngp初探
- CSP certification 202203-2 travel plan (multiple solutions)
- 杰理之更准确地确定异常地址【篇】
- 構建元宇宙時代敏捷制造的九種能力
- Sim Api User Guide(7)
猜你喜欢
Read LSTM (long short term memory)
0704、ansible----01
Custom login failure handling
Failureforwardurl and failureurl
構建元宇宙時代敏捷制造的九種能力
正大国际讲解道琼斯工业指数到底是什么?
Juc并发编程06——深入剖析队列同步器AQS源码
2022年制冷与空调设备运行操作考试练习题及模拟考试
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
第120章 SQL函数 ROUND
随机推荐
杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
CSP certification 202203-2 travel plan (multiple solutions)
Career planning and implementation in the era of meta universe
最长公共前串
Leetcode22:括号生成
Depth selector
Sim Api User Guide(6)
杰理之用户如何最简单的处理事件【篇】
0704、ansible----01
lnmp的配置
2022年上海市安全员C证考试题库及答案
Art template template engine
Sim Api User Guide(8)
Prefix sum of integral function -- Du Jiao sieve
[hdu6833] a very easy math problem
Jerry's factors that usually affect CPU performance test results are: [article]
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
MapReduce计算流程详解
Configuration of LNMP
杰理之通常影响CPU性能测试结果的因素有:【篇】