当前位置:网站首页>[leetcode] day93 - intersection of two arrays II
[leetcode] day93 - intersection of two arrays II
2022-07-03 06:22:00 【Upside down, it's a circle】
subject
350. Intersection of two arrays II【 Simple 】
Answer key
Hashtable
Statistics in hash table nums1 Elements and quantities of , And then nums2 Search for
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
int n1=nums1.length,n2=nums2.length;
int[] res=new int[Math.min(n1,n2)];
Map<Integer,Integer>hashMap=new HashMap<>();
// Hash table storage nums1 Number of elements of
for(int i=0;i<n1;i++){
if(hashMap.containsKey(nums1[i])){
int count=hashMap.get(nums1[i])+1;
hashMap.put(nums1[i],count);
}
else
hashMap.put(nums1[i],1);
}
//nums2 Find the intersection in the hash table
int k=0;
for(int i=0;i<n2;i++){
if(hashMap.containsKey(nums2[i])){
int count=hashMap.get(nums2[i]);
if(count>0){
hashMap.put(nums2[i],count-1);
res[k++]=nums2[i];
}
}
}
// Array truncation
return Arrays.copyOfRange(res, 0, k);
}
}
Time complexity : O ( m + n ) O(m+n) O(m+n)
Spatial complexity : O ( m i n ( m , n ) ) O(min(m,n)) O(min(m,n))
Sort + Double pointer
First, sort the two arrays ,
Then according to nums1[i] and nums2[j] Size of move pointer i and j The location of , Whose element is smaller, whose pointer moves back one bit ; If the elements are equal , Then it will be recorded in the result array
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
int n1=nums1.length,n2=nums2.length;
int[] res=new int[Math.min(n1,n2)];
// Array sorting
Arrays.sort(nums1);
Arrays.sort(nums2);
// Double pointer
int i=0,j=0,k=0;
while(i<n1&&j<n2){
if(nums1[i]==nums2[j]){
res[k++]=nums1[i++];
j++;
}
else if(nums1[i]<nums2[j])
i++;
else
j++;
}
// Array truncation
return Arrays.copyOfRange(res, 0, k);
}
}
Time complexity : O ( m l o g m + n l o g n ) O(mlogm+nlogn) O(mlogm+nlogn), The time complexity of sorting
Spatial complexity : O ( m i n ( m , n ) ) O(min(m,n)) O(min(m,n)), Create an array of return values res, Its length is the length of the shorter array .
Although method two looks a little smarter , But the actual complexity is not as good as method 1 …
边栏推荐
猜你喜欢
GPS坐标转百度地图坐标的方法
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Chapter 8. MapReduce production experience
【系统设计】邻近服务
Kubesphere - Multi tenant management
使用conda创建自己的深度学习环境
YOLOV1学习笔记
CKA certification notes - CKA certification experience post
Oauth2.0 - user defined mode authorization - SMS verification code login
Kubernetes notes (IV) kubernetes network
随机推荐
Mysql database binlog log enable record
有意思的鼠標指針交互探究
Chapter 8. MapReduce production experience
Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
Floating menu operation
Apifix installation
Phpstudy setting items can be accessed by other computers on the LAN
智牛股--03
Kubernetes notes (III) controller
Simple understanding of ThreadLocal
MySQL帶二進制的庫錶導出導入
从 Amazon Aurora 迁移数据到 TiDB
Interface test weather API
Project summary --2 (basic use of jsup)
Kubesphere - set up redis cluster
堆排序和优先队列
剖析虚幻渲染体系(16)- 图形驱动的秘密
After the Chrome browser is updated, lodop printing cannot be called
YOLOV3学习笔记
Kubernetes notes (IX) kubernetes application encapsulation and expansion