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 ...
随机推荐
- QT软键盘
如何实现鼠标单击弹出软键盘 默认情况下,如果当前编辑框无焦点,则需要鼠标点击两次才弹出软键盘,其中第一次是让该编辑框获得焦点,第二次点击才弹出软键盘: 如果当前编辑框已经获得焦点,则点击一次就会弹出软 ...
- Java基础知识强化之IO流笔记36:InputStreamReader/OutputStreamWriter 复制文本文件案例
1. 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中. 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader 目的地: b.t ...
- javascript进击(四)HTML DOM
HTML DOM (文档对象模型) 什么是DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了访问 HTML 和 XML 文档的标准. W3C 文档对象模型 (DOM) 是中立于平台和语 ...
- .Net Framework 各个版本新特性总结 (一)
.Net Framework 4.5 新特性 最近面试时又看到有问.Net Framework 新特性的问题,一时被问到了.平时也是拿起来就用,新版本出来了,新特性也就是瞄一眼,也没去仔细查看.这次干 ...
- Asp.net日期字符串格式化显示
我们经常会遇到对时间进行转换,达到不同的显示效果,默认格式为:2006-6-6 14:33:34 如果要换成成200606,06-2006,2006-6-6或更多的格式该怎么办呢?这里将要用到:Dat ...
- (转)HttpHandler与HttpModule的理解与应用
神秘的HttpHandler与HttpModule 大学时候我是从拖控件开始学习 asp.net的,对.net的很多类库对象都不是很了解.所以看到大家写一些个性的asp.net名词,就感觉asp.ne ...
- AppDelegate 方法详解
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- C#一些小技巧
在C#实现类似Typedef的所有功能 Typedef这个关键字,是比较好用的东西,因为有时候我们需要使用一些别名来帮助我们记忆某些结构体或者类的共用.(个人觉得这是C与C++唯一能吸引我的东西)为了 ...
- 利用ORACLE ADV 功能完成SQL TUNING 调优(顾问培训) “让DBA失业还是解脱?”
oracle自动判断SQL性能功能. 11G的ADV,建议.SNAPSHOT,数据集合, 存储在oracle sys $_开头的表(10几条). 创建SNAPSHOT时选择天数, 默认14天. sq ...
- Angular2中的host
要将Angular组件渲染成DOM中的某种东西,你需要在Angular组件中结合一个DOM元素,我们称这些叫host元素. 一个组件可以用以下方式于其host DOM元素进行交互 它可以监听其事件. ...