当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
ETL为什么经常变成ELT甚至LET?
3-全局异常处理
Interface test actual project 03: execute test cases
Gin service exit
Excel file reading and writing (creation and parsing)
Meta configuration item of route
Error 1045 (28000) access denied for user 'root' @ 'localhost' solution
反射reflect
CMOS芯片制造全工艺流程
请问flink支持sqlServer数据库么?获取sqlServer数据库的变化
CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
时钟树综合(一)
logback 中FileAppender具有什么功能呢?
彻底搞懂kubernetes调度框架与插件
Can I specify memory parameters in SQL statements?
Student achievement ranking system based on C language design
Practice of online problem feedback module (XVII): realize the online download function of excel template
路由中的生命周期钩子 - activated与deactivated
Operator3-设计一个operator
logback filter过滤器简介说明