当前位置:网站首页>【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,其长度为较短的数组的长度。
虽然看上去方法二看上去更聪明一点,但实际复杂度却不如方法一…
边栏推荐
- . Net program configuration file operation (INI, CFG, config)
- 从小数据量分库分表 MySQL 合并迁移数据到 TiDB
- tabbar的设置
- Migrate data from Amazon aurora to tidb
- SVN分支管理
- Cesium 点击获三维坐标(经纬度高程)
- Tabbar settings
- 【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
- Oauth2.0 - using JWT to replace token and JWT content enhancement
- [system design] proximity service
猜你喜欢

Use abp Zero builds a third-party login module (I): Principles

Understand the first prediction stage of yolov1

Disruptor learning notes: basic use, core concepts and principles

Phpstudy setting items can be accessed by other computers on the LAN

Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
![[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)](/img/a4/00aca72b268f77fe4fb24ac06289f5.jpg)
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)

Oauth2.0 - user defined mode authorization - SMS verification code login

Oauth2.0 - use database to store client information and authorization code

YOLOV1学习笔记

智牛股项目--04
随机推荐
Judge whether the date time exceeds 31 days
Mysql database table export and import with binary
Zhiniu stock project -- 04
从 Amazon Aurora 迁移数据到 TiDB
Une exploration intéressante de l'interaction souris - pointeur
Synthetic keyword and NBAC mechanism
Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
Convolution operation in convolution neural network CNN
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
Disruptor learning notes: basic use, core concepts and principles
项目总结--04
代码管理工具
When PHP uses env to obtain file parameters, it gets strings
Code generator - single table query crud - generator
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
The most classic 100 sentences in the world famous works
Oracle database synonym creation
Cesium 点击获三维坐标(经纬度高程)
Oauth2.0 - user defined mode authorization - SMS verification code login