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)的更多相关文章

  1. 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 ...

  2. leetcode 第4题 Median of Two Sorted Arrays

    class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int&g ...

  3. 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 ...

  4. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Median of Two Sorted Arrays(Java)

    求2个数组的中位数 方法很多 但是时间复杂度各异 1利用数组copy方法先融合两个数组,然后排序,找出中位数 import java.lang.reflect.Array; import java.u ...

  10. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

随机推荐

  1. axis2调用webservice

    public static long TIMEOUTINMILLISECONDS=100000; /** * 调用webservice * @param url webserviceURL * @pa ...

  2. Java 实现顺序查找

    package search; import java.util.Scanner; /*通常把查找过程中对关键字的平均比较次数,也叫平均查找长度(ASL)作为衡量一个查找算法效率优劣的标准: * AS ...

  3. mysqldump导出csv格式文件

    mysqldump bstar -t  -T/tmp Nvr  --fields-enclosed-by=\" --fields-terminated-by=, --where=" ...

  4. Tcl在Vivado中的使用

    http://blog.chinaaet.com/detail/36014 Vivado是Xilinx最新的FPGA设计工具,支持7系列以后的FPGA及Zynq 7000的开发.与之前的ISE设计套件 ...

  5. Ubuntu server搭建vsftpd小记

    Ubuntu server中搭建vsftpd小记 <h1> 在Ubuntu server中安装vsftpd</h1> sudo apt-get install vsftpd & ...

  6. 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

  7. Library cache lock 故障解决一例

    今天收到同事电话,说是数据库中一张名为acct_balance进行操作是奇慢,第一反映是不是扫行计划有问题,结果我错了,现将过程记录下来. 用pl/sql连上数据库情况:1.对acct_balance ...

  8. 09.25日记(2014年9月25日23:22:06)用java这么多年面向对象我真的懂了吗,测试先行理念会玩吗

    二胡 (1)应该找些书来看看,工作N年并不代表就有N年的工作经验. (2)DiaTransit02,DiaDept02,DiaAirport02,DiaHighway02.都具有x,y属性为何不设计一 ...

  9. HDU 1300 Pearls (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题目大意:珠宝店有100种不同质量的珍珠,质量越高价钱越高,为了促进销售,每买一种类型的珍珠,要 ...

  10. stack around the variable “XX” was corrupted

    晚上花了几个小时fix了这个恼人的BUG!“在变量XX周围的堆栈已损坏” 在网上找到的解释是: 把“project->配置属性->c/c++->代码生成->基本运行时检查 设置 ...