当前位置:网站首页>Leetcode- intersection of two arrays ii- simple
Leetcode- intersection of two arrays ii- simple
2022-06-13 05:47:00 【AnWenRen】
title :350 Intersection of two arrays II- Simple
subject
Here are two arrays of integers nums1 and nums2 , Please return the intersection of two arrays as an array . Returns the number of occurrences of each element in the result , It should be consistent with the number of occurrences of elements in both arrays ( If the number of occurrences is inconsistent , Then consider taking the smaller value ). You can ignore the order of the output results .
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]
Tips
1 <= nums1.length, nums2.length <= 10000 <= nums1[i], nums2[i] <= 1000
Advanced
What if the given array has been ordered ? How will you optimize your algorithm ?
If nums1 Size ratio nums2 Small , Which method is better ?
If nums2 Elements of are stored on disk , Memory is limited , And you can't load all the elements into memory at once , What are you gonna do? ?
Code Java
public int[] intersection(int[] nums1, int[] nums2) {
HashMap map1 = new HashMap();
HashMap map2 = new HashMap();
HashMap map3 = new HashMap();
// hold nums1 Fill to map1 k yes num v Is the number of occurrences
for (int i = 0; i < nums1.length; i++) {
Object o1 = map1.get(nums1[i]);
if (o1 != null) {
int count = (int) o1 + 1;
map1.put(nums1[i], count);
} else {
map1.put(nums1[i], 1);
}
}
// hold nums2 Fill to map2 k yes num v Is the number of occurrences
for (int i = 0; i < nums2.length; i++) {
Object o2 = map2.get(nums2[i]);
if (o2 != null) {
int count = (int) o2 + 1;
map2.put(nums2[i], count);
} else {
map2.put(nums2[i], 1);
}
}
// Traverse nums2 Of map2 mapping nums1 Of map1 Have the same elements Put in map3 take v Put small elements into map1
Set set = map2.entrySet();
for (Object o : set) {
Map.Entry entry = (Map.Entry) o;
Object key = entry.getKey();
int k = (int) key;
Object o1 = map1.get(k);
if (o1 != null) {
int x1 = (int) o1;
int x2 = (int) entry.getValue();
if (x1 > x2){
map3.put(k, x2);
} else {
map3.put(k, x1);
}
}
}
// hold map1 Put the elements in Array
ArrayList<Integer> arrayList = new ArrayList<Integer>();
set = map3.entrySet();
for (Object o : set) {
Map.Entry entry = (Map.Entry) o;
int sum = (int) entry.getValue();
for (int i = 0; i < sum; i++) {
arrayList.add((int)entry.getKey());
}
}
int[] result = new int[arrayList.size()];
for (int i = 0; i < result.length; i++) {
result[i] = arrayList.get(i);
}
return result;
}
// Advanced
public int[] intersection1(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
// Double pointer
ArrayList<Integer> arrayList = new ArrayList<Integer>();
int n = nums1.length;
int m = nums2.length;
for (int i = 0, j = 0; i < n && j < m;) {
if (nums1[i] == nums2[j]) {
arrayList.add(nums1[i]);
i ++;
j ++;
} else if (nums1[i] > nums2[j]) {
j ++;
} else {
i++;
}
}
int[] result = new int[arrayList.size()];
for (int i = 0; i < result.length; i++) {
result[i] = arrayList.get(i);
}
return result;
}
边栏推荐
- Unity游戏优化(第2版)学习记录7
- Feel the power of shardingsphere JDBC through the demo
- Database design
- Etcd understanding of microservice architecture
- Sentinel series integrates Nacos and realizes dynamic flow control
- redis
- Standard input dialog for pyqt5 qinputdialog
- Quartz database storage
- August 15, 2021 another week
- 若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
猜你喜欢

SPI primary key generation strategy for shardingsphere JDBC

Automatic database backup (using Navicat)

Comment procéder à l'évaluation des algorithmes

Use the browser to cut the entire page (take chrome as an example)

NVIDIA Jetson nano/xavier NX capacity expansion tutorial

C calls the API and parses the returned JSON string

Three paradigms of MySQL

ZABBIX proxy, sender (without agent monitoring), performance optimization

Building a stand-alone version of Nacos series

MySQL fuzzy query and sorting by matching degree
随机推荐
Explanation of sentinel series' features, composition and deployment
Automatic database backup (using Navicat)
Agile conflicts and benefits
2021.9.30 learning log -postman
Mysql database crud operation
System performance monitoring system
Nacos series registry principle and source code analysis
Integer tips
Feel the power of shardingsphere JDBC through the demo
A simple recursion problem of linked list
Four shardingsphere JDBC sharding strategies
2021.9.30学习日志-postman
OpenGL馬賽克(八)
Compilation croisée helloworld avec cmake
Quartz basic use
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Mobile end adaptation scheme
C calls the API and parses the returned JSON string
Mysql database backup and restore:
redis