当前位置:网站首页>【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,其长度为较短的数组的长度。
虽然看上去方法二看上去更聪明一点,但实际复杂度却不如方法一…
边栏推荐
- When PHP uses env to obtain file parameters, it gets strings
- Jedis source code analysis (I): jedis introduction, jedis module source code analysis
- Read blog type data from mysql, Chinese garbled code - solved
- GPS坐标转百度地图坐标的方法
- [system design] proximity service
- YOLOV1学习笔记
- Understand the first prediction stage of yolov1
- Tabbar settings
- Migrate data from Amazon aurora to tidb
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
猜你喜欢

Kubernetes notes (I) kubernetes cluster architecture

Use selenium to climb the annual box office of Yien

YOLOV2学习与总结

Redis cluster creation, capacity expansion and capacity reduction

Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)

Project summary --2 (basic use of jsup)

【系统设计】邻近服务

Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide

技术管理进阶——你了解成长的全貌吗?

Kubesphere - Multi tenant management
随机推荐
Use selenium to climb the annual box office of Yien
Alibaba cloud OOS file upload
The win7 computer can't start. Turn the CPU fan and stop it
IE browser flash back, automatically open edge browser
Kubernetes notes (VII) kuberetes scheduling
Apifix installation
Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
Loss function in pytorch multi classification
Creating postgre enterprise database by ArcGIS
Synthetic keyword and NBAC mechanism
Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
Mysql database binlog log enable record
JMeter linked database
YOLOV3学习笔记
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
Kubernetes cluster environment construction & Deployment dashboard
BeanDefinitionRegistryPostProcessor
Detailed explanation of contextclassloader
Disruptor learning notes: basic use, core concepts and principles
Analysis of Clickhouse mergetree principle