当前位置:网站首页>Hashtable hash table and statistics 594, 350, |554, 609, 454, 18
Hashtable hash table and statistics 594, 350, |554, 609, 454, 18
2022-04-22 14:13:00 【Borrow some hair】
350. Intersection of two arrays II
Given two arrays , Write a function to calculate their intersection .
Example 1:
Input :nums1 = [1,2,2,1], nums2 = [2,2]
Output :[2,2]
Example 2:
Input :nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output :[4,9]
explain :
The number of occurrences of each element in the output result , Should be consistent with the minimum number of occurrences of an element in both arrays .
We can ignore the order of output results .
Answer key :
1) Two set After de duplication, traverse one of them . because set.toArray There are some problems that have not been realized
2)hashmap, Same in essence 1
3) Double pointer after sorting , Sort the two arrays separately , Two pointers traverse two arrays from scratch , Equal records
sloution:
HashMap<Integer,Integer> map = new HashMap<>();
//1. Statistics nums1 Number of occurrences of each element in
for (int i = 0; i < nums1.length; i++) {
// When Map There's this in the collection key when , Just use this key value , If not, use the default value defaultValue
int count = map.getOrDefault(nums1[i],0)+1;
map.put(nums1[i],count);
}
//2. Traverse nums2 Look for the presence of map The elements in are updated at the same time map Of key The number of times
int[] result = new int[nums1.length];// Since it's the intersection of two arrays , Then the length must be less than or equal to any array
int index = 0;
for (int target : nums2) {
int count = map.getOrDefault(target,0);
// Proof element target Exist in map, Just explain nums2 Medium target Is and nums1 Elements with intersection
if (count > 0){
result[index++] = target;// Add to result set
count --;
map.put(target,count);// to update map
}
}
// Copy the specified range of the specified array to a new array
return Arrays.copyOfRange(result,0,index);
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
// Sort two arrays .
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;
// As long as an array is traversed, it will jump out of the loop .
while (index1 < len1 && index2 < len2) {
// Traversing two arrays , Whoever is young moves his pointer , Equal to record and move the pointer at the same time .
if (nums1[index1] < nums2[index2]) {
index1++;
} else if (nums1[index1] > nums2[index2]) {
index2++;
} else {
ans[index] = nums1[index1];
index1++;
index2++;
index++;
}
}
// After traversal, return the result array of repeated element length .
return Arrays.copyOfRange(ans, 0, index);
}
}
594. The longest harmonious subsequence
A harmonious array means that the difference between the maximum and minimum values of elements in an array is just 1. Now? , Given an array of integers , You need to find the length of the longest harmonious subsequence among all possible subsequences .
Example 1:
Input : [1,3,2,2,5,2,3,7]
Output : 5
reason : The longest harmonious array is :[3,2,2,2,3].
Answer key :
1)hash mapping
2) Double pointer after sorting Similar to sliding window
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;
}
}
版权声明
本文为[Borrow some hair]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221411324323.html
边栏推荐
- 【终于等到你】微信转发语音的方法 - 语音信息转发
- TensorFlow-ValueError: ‘images‘ contains no shape
- TensorFlow-ValueError: ‘images‘ contains no shape
- Huawei cloud media Zha Yong: Huawei cloud's technical practice in the field of video AI transcoding
- Shiro's cache management
- Fastdfs installation and configuration
- makefile 调用bash脚本遇到的坑
- QT create window flash back problem
- Move blog to CSDN
- How to get tuphub Today's hot list and heat?
猜你喜欢

FastDFS 安装和配置
![uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件](/img/3b/2f371eab7d2f9e976dcd4612a19518.png)
uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件

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

Thoughts on dealing with high concurrency problems

The more "intelligent" the machine is, the easier it is for data taggers to be eliminated? Manfu Technology

Understand C language -- string, escape character, comment

Openmldb pulsar connector: efficiently connect real-time data to feature Engineering

Lors de l'obtention d'une valeur dans la base de données, la base de données a une valeur, mais elle est vide.

多线程初阶

What does log desensitization mean? Why do you do log desensitization?
随机推荐
多线程进阶
redis连接工具无法连上docker中redis
关于半导体的8个奇特事实
uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件
Blocking queue-
日志脱敏是什么意思?为什么要做日志脱敏?
Brief description of three elements of LAN characteristics
uniapp运行到小程序模拟器的方法 - uniapp开启微信开发者工具预览支持 - HBuilderX
Understanding of redis
Shiro之缓存管理
Binarytree练习二叉树层序遍历 || 用队列实现层序遍历
Operation of 2022 chemical automation control instrument examination practice question simulation examination platform
Binarytree练习二叉树序列化反序列化606、331、652、297
Osgearth configuring Map Resources
What does log desensitization mean? Why do you do log desensitization?
ICLR2022杰出论文奖出炉,清华、人大获奖,浙大提名
[zeekr_tech] Introduction to ros/ros 2
双指针头尾指针 167、|345、680、15、16、18、11、42
Detailed explanation of channel implementation of cocoyaxi Library
Translucenttb Chinese interface - Chinese menu - how to use - win11 taskbar transparent effect