当前位置:网站首页>leetcode--两个数组交集2
leetcode--两个数组交集2
2022-07-16 20:33:00 【Peihj2021】
leetcode–两个数组交集2
题目描述

思路分享
两个数组的交集2与两个数组的交集1的区别在于交集1不准重复但是交集2可以重复,
1、我们选定数组长度较短的一个进行hashmap赋值,如何选择长度较短的一个本题目中用了递归算法保证nums1一直都是最短的。其中k为数值,v为数值出现的次数。
2、在nums1用hashmap赋值以后我们还需要对nums2进行遍历,在nums2遍历的过程中我们需要通过nums1的hashmap的value值进行判断nums1中有没有出现这个数值。
3、如果hashmap存在那么将数值存储进入数组中,并将相应的hashmap的value值进行减一。
代码
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length){
return intersect(nums2,nums1);
}
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i : nums1) {
if (hashMap.containsKey(i)){
hashMap.put(i,hashMap.get(i)+1);
}else {
hashMap.put(i,1);
}
}
int[] res = new int[nums1.length];
int index = 0;
for (int i : nums2) {
Integer count = hashMap.getOrDefault(i, 0);
if (count >= 1){
res[index++] = i;
count--;
if (count > 0){
hashMap.put(i,count);
}else {
hashMap.remove(i);
}
}
}
return Arrays.copyOfRange(res,0,index);
}
}

参考
https://leetcode.cn/problems/intersection-of-two-arrays-ii/
边栏推荐
- Peking University and Microsoft jointly proposed a super time series representation learning framework, which significantly improved the effect of multiple time series tasks
- A New Optimizer Using Particle Swarm Theory
- [basic service] [database] MySQL master-slave replication deployment and configuration
- Dictionary tree leetcode seven hundred and forty-five
- Developers share the initial experience of tinyms, a high-level API tool of mindspire!
- Modify server password
- KDD 2017 | metapath2vec: scalable representation learning of heterogeneous graphs
- Voice conversion mainly involves technical records
- Detailed explanation of bean's life cycle
- LCD 显示撕裂解决
猜你喜欢

SIEMENS模块6DD1661-0AE1

Source code analysis of ArrayList

KDD 2017 | metapath2vec: scalable representation learning of heterogeneous graphs

Modify server password

Alicloud3 build WordPress

The observation returned by the 'reset()' method is not contained with the

除了长安,这四个国产品牌也用“雷克萨斯脸”,中国设计倒退了?
![(manual) [sqli-labs58-61] limit the injection times: error injection, get injection, character / number type](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(manual) [sqli-labs58-61] limit the injection times: error injection, get injection, character / number type

开发者分享|手写算子没那么难,教你用MindSpore实现自适应平均池化算子!

Remember once, ants were abused on all sides. The water was too deep. Have you built the ferry across the river?
随机推荐
Two stack implementation queue and two queue implementation stack (JS)
私域运营很火,私域运营是否适合所有企业?
UCOSII里面为什么调用OS_ENTER_CRITICAL()或OS_EXIT_CRITICAL()会出错,出错信息为:undeclared identifier `cpu_sr‘
原装rexroth比例阀4WRBA10W64-2X/G24N9Z4/M
北大&微软联合提出超强时间序列表示学习框架,显著提升多项时间序列任务效果
2022.7.14-----leetcode. seven hundred and forty-five
LINGO运算符和内置函数
2-conan 二进制包依赖管理方案
【小程序项目开发-- 京东商城】uni-app之商品列表页面 (上)
How to deal with time series event data? [doctoral thesis of Munich University of technology] neural time series point process (ntpp): continuous time event data modeling
博途PLC模糊PID在双容水箱液位控制上的应用(模糊和普通PID切换功能FB)
parker派克柱塞泵PV140R1K1T1NMMC
哪个证券公司开户股票手续费低,哪个股票开户安全
记一次蚂蚁金服四面遭虐,面试水太深,过河的渡船你造好了吗?
Quickly solve the problem of error or garbled code when inserting Chinese data into mysql
Dajiang school recruitment evaluation question -- round robin problem
Leetcode 47. Full arrangement II
After reading this article, I will teach you to play with vulnhub, the penetration test target machine -- evilbox one
创建型模式之享元模式
Transfer function