当前位置:网站首页>HashTable哈希表与统计594、350、|554、609、454、18
HashTable哈希表与统计594、350、|554、609、454、18
2022-04-22 14:12:00 【借点头发吧】
350. 两个数组的交集 II
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[4,9]
说明:
输出结果中每个元素出现的次数,应与元素在两个数组中出现次数的最小值一致。
我们可以不考虑输出结果的顺序。
题解:
1)两个set去重后遍历其中一个。因为set.toArray有点问题没有实现
2)hashmap,实质同1
3)排序后双指针,分别对两个数组进行排序,两个指针从头遍历两个数组,相等的记录
sloution:
HashMap<Integer,Integer> map = new HashMap<>();
//1.统计nums1中各个元素的出现次数
for (int i = 0; i < nums1.length; i++) {
//当Map集合中有这个key时,就使用这个key值,如果没有就使用默认值defaultValue
int count = map.getOrDefault(nums1[i],0)+1;
map.put(nums1[i],count);
}
//2. 遍历nums2查找存在map中的元素同时更新map的key的次数
int[] result = new int[nums1.length];//既然是两数组的交集,那么长度一定小于等于任意一个数组
int index = 0;
for (int target : nums2) {
int count = map.getOrDefault(target,0);
//证明元素target存在于map,就说明nums2中的target是和nums1有交集的元素
if (count > 0){
result[index++] = target;//添加到结果集中
count --;
map.put(target,count);//更新map
}
}
//将指定的数组指定的范围复制到一个新的数组中
return Arrays.copyOfRange(result,0,index);
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
// 对两个数组进行排序。
Arrays.sort(nums1);
Arrays.sort(nums2);
int len1 = nums1.length, len2 = nums2.length;
int[] ans = new int[Math.min(len1, len2)];
int index1 = 0, index2 = 0, index = 0;
// 只要有一个数组遍历完就跳出循环。
while (index1 < len1 && index2 < len2) {
// 遍历两个数组,谁小就移动谁的指针,相等就记录并同时移动指针。
if (nums1[index1] < nums2[index2]) {
index1++;
} else if (nums1[index1] > nums2[index2]) {
index2++;
} else {
ans[index] = nums1[index1];
index1++;
index2++;
index++;
}
}
// 遍历完成返回重复元素长度的结果数组。
return Arrays.copyOfRange(ans, 0, index);
}
}
594. 最长和谐子序列
和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度。
示例 1:
输入: [1,3,2,2,5,2,3,7]
输出: 5
原因: 最长的和谐数组是:[3,2,2,2,3].
题解:
1)hash映射
2)排序后双指针 类似滑动窗口
sloution:
class Solution {
public int findLHS(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int LL = 0;
for(int i : nums)
{
if(map.containsKey(i))
map.put(i, map.get(i)+1);
else
map.put(i, 1);
if(map.containsKey(i - 1))
LL = Math.max(LL, map.get(i) + map.get(i - 1));
if(map.containsKey(i + 1))
LL = Math.max(LL, map.get(i) + map.get(i + 1));
}
return LL;
}
}
版权声明
本文为[借点头发吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47866171/article/details/108219245
边栏推荐
- LeetCode-819 最常见的单词
- 华为云媒体査勇:华为云在视频AI转码领域的技术实践
- 获取数据库中数值时,数据库有值,却为空??
- [PHP] MVCs concept (easy to understand)
- FastDFS 安装和配置
- Customization of AoYa Dao community pledge mining DAPP system
- Leetcode -- the shortest distance between characters
- OceanBase的超级用户 - 系统租户sys
- Su Xiaohong C Language Programming Chapter IV and V knowledge summary
- Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
猜你喜欢

Translucenttb Chinese interface - Chinese menu - how to use - win11 taskbar transparent effect

多线程进阶

Blocking queue-

獲取數據庫中數值時,數據庫有值,卻為空??

二月份,我靠这一份PDF文档面试BAT,没想到竟然收到了5个offer

线程池--
![[paper notes] vision transformers for dense prediction](/img/71/aaf1509237192923ee71a5148e5502.png)
[paper notes] vision transformers for dense prediction

A solution to the problem of buying and selling stocks by force deduction

万元礼品奖池!玩转「Lighthouse」有奖征文来袭

银行为什么要上堡垒机?选择哪家好?有案例吗?
随机推荐
uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件
Is it safe to open an account in Zhujiang futures?
redis连接工具无法连上docker中redis
QT explorer and Use of QRC file
In the source code of Vue cache compilation results, the template is used as the cache key, and the template written by the user is so long. Is this appropriate
Multithreading primary
Seven years of "one sword" and standing at the edge of the cloud, how to accelerate the digital transformation of enterprises?
2022 tea artist (intermediate) examination simulation 100 questions and simulation examination
Markdown图标
uniapp运行到微信开发者工具中小程序端页面空白的解决办法
处理高并发问题思路
银行为什么要上堡垒机?选择哪家好?有案例吗?
TensorFlow-ValueError: ‘images‘ contains no shape
Getting started with BCC
[summary of Kunpeng migration and practice Posts] the first play~~
Shiro之缓存管理
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Leetcode-819 the most common word
2022危险化学品经营单位安全管理人员操作证考试题及模拟考试
Binary Tree递归||二叉树展开为链表 530. 二叉搜索树的最小绝对差