当前位置:网站首页>【LeetCode】Day93-两个数组的交集 II
【LeetCode】Day93-两个数组的交集 II
2022-07-03 06:15:00 【倒过来是圈圈】
题目
题解
哈希表
哈希表中统计nums1的元素和数量,再在nums2中查找
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<>();
//哈希表存储nums1的元素个数
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在哈希表中找交集
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];
}
}
}
//数组截取
return Arrays.copyOfRange(res, 0, k);
}
}
时间复杂度: O ( m + n ) O(m+n) O(m+n)
空间复杂度: O ( m i n ( m , n ) ) O(min(m,n)) O(min(m,n))
排序+双指针
首先对两个数组排序,
之后根据nums1[i]和nums2[j]的大小移动指针 i 和 j 的位置,谁的元素小谁的指针后移一位;若元素相等,则记入结果数组
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
int n1=nums1.length,n2=nums2.length;
int[] res=new int[Math.min(n1,n2)];
//数组排序
Arrays.sort(nums1);
Arrays.sort(nums2);
//双指针
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++;
}
//数组截取
return Arrays.copyOfRange(res, 0, k);
}
}
时间复杂度: O ( m l o g m + n l o g n ) O(mlogm+nlogn) O(mlogm+nlogn),排序所需的时间复杂度
空间复杂度: O ( m i n ( m , n ) ) O(min(m,n)) O(min(m,n)),为返回值创建一个数组 res,其长度为较短的数组的长度。
虽然看上去方法二看上去更聪明一点,但实际复杂度却不如方法一…
边栏推荐
- Convolution operation in convolution neural network CNN
- MySQL带二进制的库表导出导入
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- BeanDefinitionRegistryPostProcessor
- Disruptor learning notes: basic use, core concepts and principles
- Cesium entity (entities) entity deletion method
- Jedis source code analysis (II): jediscluster module source code analysis
- Use @data in Lombok to simplify entity class code
- CKA certification notes - CKA certification experience post
- Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
猜你喜欢
智牛股--03
Use abp Zero builds a third-party login module (I): Principles
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Kubesphere - build Nacos cluster
Mysql
Kubernetes notes (VII) kuberetes scheduling
Detailed explanation of contextclassloader
23 design models
How to scan when Canon c3120l is a network shared printer
随机推荐
Project summary --04
Cannot get value with @value, null
Zhiniu stock project -- 04
Synthetic keyword and NBAC mechanism
Convolution operation in convolution neural network CNN
表达式的动态解析和计算,Flee用起来真香
Creating postgre enterprise database by ArcGIS
Printer related problem record
Svn branch management
Shell conditional statement
Leetcode problem solving summary, constantly updating!
The most classic 100 sentences in the world famous works
Kubernetes notes (IX) kubernetes application encapsulation and expansion
YOLOV3学习笔记
项目总结--2(Jsoup的基本使用)
智牛股项目--05
Introduction to software engineering
Virtual memory technology sharing
Detailed explanation of contextclassloader
BeanDefinitionRegistryPostProcessor