当前位置:网站首页>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
边栏推荐
- Thoroughly understand kubernetes scheduling framework and plug-ins
- Scala higher order (IX): pattern matching in Scala
- Kubernetes (V) -- deploy kubernetes dashboard
- logback简介及引入方法
- Vite3.0都发布了,你还能卷得动吗(新特性一览)
- Redis基础篇
- QT基础第二天(2)qt基础部件:按钮类,布局类,输出类,输入类,容器等个别举例
- 第7节-程序的编译(预处理操作)+链接
- Personal blog system (with source code)
- Vscode remote debugging PHP solution through remotessh and Xdebug
猜你喜欢

WPF simple login page completion case

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

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法

一篇长文---深入理解synchronized

Summary of OCR optical character recognition methods

Ethernet interface introduction

MySQL advanced (Advanced) SQL statement (I)

MySQL uses the client and select methods to view the summary of blob type fields

个人博客系统(附源码)

MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
随机推荐
利用C语言巧妙实现棋类游戏——三子棋
Ansible中的变量及加密
Vue router route cache
路由中的生命周期钩子 - activated与deactivated
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?
Problems encountered in vmware16 installing virtual machines
logback filter过滤器简介说明
NPM install reports an error NPM err could not resolve dependency NPM err peer
Remote invocation of microservices
Synchronous / asynchronous, blocking / non blocking and IO
CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
Personal blog system (with source code)
Life cycle hooks in routing - activated and deactivated
Unity sends a post request to the golang server for parsing and returning
力扣(LeetCode)209. 长度最小的子数组(2022.07.28)
5-integrate swagger2
能在SQL 语句中 指定 内存参数吗?
How to use GS_ Expansion expansion node
Meta configuration item of route