当前位置:网站首页>Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
2022-07-29 07:23:00 【Do you eat oil cake】
4. Find the median of two positive arrays
Title Description :

1. Merge to find the median
Ideas : The solution we thought of at the beginning of getting this problem must be violent solution , Directly combine the two arrays to find the median .
When the merged array is even , Then put the length/2 Elements and number length-1/2 Elements are averaged ; If it's an odd number , Then go directly to No length/2 Elements ;
The code is as follows :
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] temp = new int[nums1.length+nums2.length];
int i = 0,j = 0,t = 0;
while (i < nums1.length && j < nums2.length){
if (nums1[i] < nums2[j]){
temp[t] = nums1[i];
i++;
t++;
}else {
temp[t] = nums2[j];
j++;
t++;
}
}
while ( i < nums1.length){
temp[t] = nums1[i];
i++;
t++;
}
while (j< nums2.length){
temp[t] = nums2[j];
j++;
t++;
}
double t1 = temp[temp.length/2];
double t2 = temp[(temp.length-1)/2];
if (temp.length % 2 == 0)
return (t1+t2) / 2.0;
else
return t2;
}
Time complexity : Traversed all arrays O(m+n)
Spatial complexity : A new array is defined ,O(m+n)
2. Do not merge ( Traverse to the median )
Ideas : The first method is to merge the arrays directly , Then find the median , In fact, when we think about it carefully, we know that we can not merge , Directly traverse to the median and directly return ; But now the problem is , When the number is odd, we can directly return to the median , But what about even numbers ?
If it's even , We need to record the number length/2 Elements and number length-1/2 Elements , We can introduce two variables ,left、right, After each traversal right Value is assigned to left(left = right), So when we traverse to the last number (length/2), You will find that just left The value of ,left= The first length-1/2 Elements ,right= The first length-1/2 Elements , So we can judge later ;
Let's look at the code :
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length,n = nums2.length;
int len = m + n;
int left = 0,right = 0;
int start1 = 0,start2 = 0; // Start subscript
for (int i = 0; i <= len/2; i++) {
left = right;
if (start1 < m && (start2 >= n || nums1[start1] < nums2[start2]))
right = nums1[start1++];
else
right = nums2[start2++];
}
if (len % 2 == 0) // Combine to an even number
return (left+right)/2.0;
else
return right;
}Time complexity : Ergodic len/2+1 Time , Still O(m+n)
Spatial complexity :O(1)
summary : Although it is positioned as a difficult problem , But the topic logic is not complicated , Just draw a picture and think about it
边栏推荐
- Full process flow of CMOS chip manufacturing
- 如何与斯堪尼亚SCANIA建立EDI连接?
- Why does ETL often become ELT or even let?
- JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?
- js第四天流程控制(if语句和switch语句)
- 利用C语言巧妙实现棋类游戏——三子棋
- 反射reflect
- Redis Basics
- Redis基础篇
- 2-统一返回类DTO对象
猜你喜欢

Redis Basics

彻底搞懂kubernetes调度框架与插件

Nodejs安装教程

Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started

女研究生做“思维导图”与男友吵架!网友:吵架届的“内卷之王”....

Student achievement ranking system based on C language design

MySQL - multi table query

ETL为什么经常变成ELT甚至LET?

Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software

2-统一返回类DTO对象
随机推荐
CDC source can quit after reading MySQL snapshot split
【OpenGL】着色器(Shader)的使用
Error 1045 (28000) access denied for user 'root' @ 'localhost' solution
Unity发送Post请求给GoLang服务端解析并返回
2-统一返回类DTO对象
npm install 时,卡住不动,五种解决方法
logback filter过滤器简介说明
Can I specify memory parameters in SQL statements?
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
用户列表 圆形头像并跟随小板块
彻底搞懂kubernetes调度框架与插件
Variables and encryption in ansible
CVPR2021| 基于自监督学习的多视图立体匹配 (CVPR2021)
vagrant box 集群 处理
WPF interface layout must know basis
MySQL uses the client and select methods to view the summary of blob type fields
Other basic monitoring items of ZABBIX
Nodejs安装教程
WPF nested layout case
0 9 布隆过滤器(Bloom Filter)