leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays
There
are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
time=378ms
accepted
<pre name="code" class="java">public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
int m=A.length,n=B.length;
int median=(m+n)/2;
if((m+n)%2==0)
return (findNum(A,0,m-1,B,0,n-1,median)+
findNum(A,0,m-1,B,0,n-1,median+1))/2;
else
return findNum(A,0,m-1,B,0,n-1,median+1);
}
/*找到已排序序列的第k个数*/
public double findNum(int A[],int leftA,int rightA,
int B[],int leftB,int rightB,int k){
//always assume alen<blen
int alen=rightA-leftA+1;
int blen=rightB-leftB+1;
if(alen>blen)
return findNum(B,leftB,rightB,A,leftA,rightA,k);
if(alen==0)
return B[leftB+k-1];
if(k==1)
return min(A[leftA],B[leftB]);
int atemp=min(k/2,alen);
int btemp=k-atemp;
if(A[leftA+atemp-1]<B[leftB+btemp-1]){
return findNum(A,leftA+atemp,rightA,B,leftB,leftB+btemp-1,k-atemp);
}else if(A[leftA+atemp-1]>B[leftB+btemp-1]){
return findNum(A,leftA,leftA+atemp-1,B,leftB+btemp,rightB,k-btemp);
}else{
return A[leftA+atemp-1];
}
}
int min(int x,int y){
return x<y?x:y;
}
}
<pre name="code" class="java"> //----------------代码切换----------------------------//
if(A[leftA+atemp-1]<B[leftB+btemp-1]){
return findNum(A,leftA+atemp,rightA,B,leftB,<strong>rightB</strong>,k-atemp);
}else if(A[leftA+atemp-1]>B[leftB+btemp-1]){
return findNum(A,leftA,<strong>rightA</strong>,B,leftB+btemp,rightB,k-btemp);
}else{
return A[leftA+atemp-1];
}
修改比较之后AB数组的结束位置,原理上前面的方法中AB的长度比后者缩短了一半,但耗时要440ms,而长度没有减半时耗时还要短一些,为379ms,这里存在一些疑问。
分析思路(此处转载自网络大牛):
这是一道非常经典的题。这题更通用的形式是,给定两个已经排序好的数组,找到两者所有元素中第 k 大的元素。O(m + n) 的解法比较直观,直接 merge 两个数组,然后求第 k
大的元素。不过我们仅仅需要第 k 大的元素,是不需要“排序”这么复杂的操作的。可以用一个计数器,记录当前已经找到第 m 大的元素了。同时我们使用两个指针 pA 和 pB,分别指向 A 和 B 数组的第一个元素,使用类似于 merge sort 的原理,如果数组 A 当前元素小,那么 pA++,同时 m++;如果数组 B 当前元素小,那么 pB++,同时 m++。最终当 m 等于 k 的时候,就得到了我们的答案,O(k)时间,O(1) 空间。但是,当 k 很接近 m + n 的时候,这个方法还是 O(m +
n) 的。
有没有更好的方案呢?我们可以考虑从 k 入手。如果我们每次都能够删除一个一定在第 k 大元素之前的元素,那么我们需要进行 k 次。但是如果每次我们都删除一半呢?由于 A 和 B
都是有序的,我们应该充分利用这里面的信息,类似于二分查找,也是充分利用了“有序”。
假设 A 和 B 的元素个数都大于 k/2,我们将 A 的第 k/2 个元素(即 A[k/2-1])和 B 的第 k/2
个元素(即 B[k/2-1])进行比较,有以下三种情况(为了简化这里先假设 k 为偶数,所得到的结论对于 k 是奇数也是成立的):
A[k/2-1] == B[k/2-1]
A[k/2-1] > B[k/2-1]
A[k/2-1] < B[k/2-1]
如果 A[k/2-1] < B[k/2-1],意味着 A[0] 到 A[k/2-1 的肯定在 A [ B 的 top k 元素的范围内,换句话说,A[k/2-1 不可能大于 A
[ B 的第 k 大元素。留给读者证明。因此,我们可以放心的删除 A 数组的这 k/2 个元素。同理,当 A[k/2-1] > B[k/2-1] 时,可以删除 B 数组的 k/2 个元素。当 A[k/2-1] == B[k/2-1] 时,说明找到了第 k 大的元素,直接返回 A[k/2-1] 或 B[k/2-1]即可。因此,我们可以写一个递归函数。那么函数什么时候应该终止呢?
当 A 或 B 是空时,直接返回 B[k-1] 或 A[k-1];当 k=1 是,返回 min(A[0], B[0]);当 A[k/2-1] == B[k/2-1] 时,返回 A[k/2-1]
或 B[k/2-1]
leetcode第四题:Median of Two Sorted Arrays (java)的更多相关文章
- LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- leetcode 第4题 Median of Two Sorted Arrays
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int&g ...
- leetcode第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- LeetCode解题笔记 - 4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- LeetCode 笔记系列一 Median of Two Sorted Arrays
题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sort ...
- Median of Two Sorted Arrays(Java)
求2个数组的中位数 方法很多 但是时间复杂度各异 1利用数组copy方法先融合两个数组,然后排序,找出中位数 import java.lang.reflect.Array; import java.u ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
随机推荐
- web工程中spring+ibatis的单元测试--转载
为了保证代码的正确,软件的质量,单元测试几乎是每个程序员都要面临的工作了;而开发中大部分的工作都涉及数据库的操作,也就是平时经常可以看到的DAO了;由于是对数据库的操作,就必然有事务的问题了;如果是启 ...
- Android开发的第一天
不管做什么开发都是有开始的,对于开发的话开始要的准备的就是开发工具了 安装开发工具配置开发工具好了不多说了现在我来说怎么样安装和配置安卓的开发工具吧 第一首先就是要下载一个JDK (Java SE ...
- 8.LNMP环境的配置
LNMP环境的配置 参照文档:https://oneinstack.com/install/ 安装文件位置:/data/soft: ```yum -y install wget screen pyth ...
- day-1
/* 倒计时就要结束了 在机房的最后一个晚上 恩 就要结束了 上午考试 下午背板 找了几个原题敲了敲 晚上zjk老妈送的饭 撑死死死死了 好吃23333 吃饭完和zjk在机房门口楼梯上聊了一会 恩 以 ...
- JQ 如何设置单选按钮问题
<input type="radio" name="db_12" value="2" checked="checked/&g ...
- select unit_timestamp(); 和select unit_timestamp("1970-1-1 08:00:00")和 select from_unixtime(1)
偶然看到MySQL的一个函数 unix_timestamp(),不明就里,于是就试验了一番. unix_timestamp()函数的作用是返回一个确切的时间点的UNIX时间戳,这个Unix时间戳是一个 ...
- JavaScript之call()&apply()
场景一:定义了一个类A,给它一个getName的方法:定义了一个类B,给它一个setName的方法:之前A只需要获取它的Name,B也只需要设置它的Name,但现在有新的需求,A和B都需要设置和获取他 ...
- Nagios-配置版
1 概念(简介) Nagios是插件式的结构,它本身没有任何监控功能,所有的监控都是通过插件进行的,因此其是高度模块化和富于弹性的.Nagios监控的对象可分为两类:主机和服务.主机通常指的是物理主 ...
- linux关闭声音
对于CentOS/Redhat/RHEL/Fedora系统,使用root身份执行:echo "alias pcspkr off" >> /etc/modprobe.co ...
- 自动构建工具Ant的使用-笔记
第一:什么是Ant? Apache Ant是一个基于Java的生成工具.据最初的创始人James Duncan Davidson的介绍,这个工具的名称是another neat tool(另一个整洁的 ...