当前位置:网站首页>【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,其长度为较短的数组的长度。
虽然看上去方法二看上去更聪明一点,但实际复杂度却不如方法一…
边栏推荐
- Important knowledge points of redis
- Interesting research on mouse pointer interaction
- Kubernetes notes (V) configuration management
- Migrate data from Amazon aurora to tidb
- Kubernetes notes (VI) kubernetes storage
- Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
- Zhiniu stock -- 03
- Leetcode solution - 02 Add Two Numbers
- Project summary --2 (basic use of jsup)
- [set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
猜你喜欢

Advanced technology management - do you know the whole picture of growth?

Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading

Zhiniu stock project -- 04

CKA certification notes - CKA certification experience post

YOLOV1学习笔记

Alibaba cloud OOS file upload

Synthetic keyword and NBAC mechanism

YOLOV3学习笔记
![[system design] proximity service](/img/4a/2e68536cbe385af1d1a591e674fbf0.png)
[system design] proximity service

Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
随机推荐
Characteristics and isolation level of database
PHP用ENV获取文件参数的时候拿到的是字符串
Get a screenshot of a uiscrollview, including off screen parts
Loss function in pytorch multi classification
Various usages of MySQL backup database to create table select and how many days are left
In depth analysis of kubernetes controller runtime
YOLOV2学习与总结
Important knowledge points of redis
使用conda创建自己的深度学习环境
Luogu problem list: [mathematics 1] basic mathematics problems
JMeter linked database
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
Mysql database
有意思的鼠标指针交互探究
Pdf files can only print out the first page
phpstudy设置项目可以由局域网的其他电脑可以访问
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
Phpstudy setting items can be accessed by other computers on the LAN
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
Bio, NiO, AIO details