当前位置:网站首页>【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,其长度为较短的数组的长度。
虽然看上去方法二看上去更聪明一点,但实际复杂度却不如方法一…
边栏推荐
- Oauth2.0 - user defined mode authorization - SMS verification code login
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
- 认识弹性盒子flex
- Es remote cluster configuration and cross cluster search
- JMeter performance automation test
- Migrate data from Mysql to tidb from a small amount of data
- How to scan when Canon c3120l is a network shared printer
- Common interview questions
- Kubernetes notes (V) configuration management
- Une exploration intéressante de l'interaction souris - pointeur
猜你喜欢

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

Oauth2.0 - explanation of simplified mode, password mode and client mode

Es remote cluster configuration and cross cluster search

Detailed explanation of contextclassloader

Code generator - single table query crud - generator

Cesium 点击获取模型表面经纬度高程坐标(三维坐标)

Convolution operation in convolution neural network CNN

Zhiniu stock project -- 04

YOLOV3学习笔记

使用conda创建自己的深度学习环境
随机推荐
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
Shell conditional statement
PMP笔记记录
Leetcode solution - 02 Add Two Numbers
Introduction to software engineering
Important knowledge points of redis
輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
23 design models
Oracle database synonym creation
Analysis of Clickhouse mergetree principle
Leetcode problem solving summary, constantly updating!
从小数据量分库分表 MySQL 合并迁移数据到 TiDB
Method of converting GPS coordinates to Baidu map coordinates
SQL实现将多行记录合并成一行
Oracle Database Introduction
Time format record
Migrate data from Amazon aurora to tidb
有意思的鼠标指针交互探究
JMeter performance automation test
Kubesphere - Multi tenant management