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. [转]使用Beaglebone Black的SPI

    分类: Beaglebone Black2013-11-24 18:21 678人阅读 评论(6) 收藏 举报 beaglebone blackbeagleboneSPIdevice tree   目 ...

  2. CentOS 6.6 yum源完全配置

    原文地址 http://blog.csdn.net/halazi100/article/details/41311837 一 yum 简介 yum,是"Yellow dog Updater, ...

  3. js 一个关于图片onload加载的事

    前几天一个项目让我头疼了很久,一个关于图片加载时的loading效果,因为不是太懂js,所以在网上各种找资料,但还是不理想,无赖苦心研究,终于有了一点眉目了,虽然个中还有一些道理不懂,至少目的达到了; ...

  4. Git 常用配置和使用

    Git:是一个分布式的源代码管理工具,Linux内核的代码就是用Git管理的所以它很强,也很快, 和 Vss/SVN比起来 本地Git初始化配置及其使用: 1. 初始化本地Git库:打开Git Bas ...

  5. Redis Cache 简介

    Microsoft Azure Redis Cache 是基于流行的开源Redis Cache 1.Microsoft Azure Redis Cache 可分为以下几个级别: Basic – 单节点 ...

  6. Web开发必备资源汇总[转]

    导读:原文来自< Best “must know” open sources to build the new Web>,译文由酷壳网陈皓整理编译< 开源中最好的Web开发的资源 & ...

  7. ASP.NET网络编程之-HTTP协议

    HTTP协议由来已久,最近复习到它,好记性不如烂笔头,在此留下自己的总结,算是为后面再要看时用吧.HTTP协议是一个在B/S架构中约束客户端(浏览器)和服务端(比较常见的是就是IIS服务器,关于IIS ...

  8. iOS开发——生成二维码——工具类

    啥也不说,直接上源码,拷过去就能用.生成二维码的工具类使用方法在ProduceQRCode.h里有示例说明 分别将下面的ProduceQRCode.h和ProduceQRCode.m对应的代码考到自己 ...

  9. js判断主流浏览器类型和版本号

    如今的互联网中,浏览器可以说是太多太多了,但是大部分都是换壳不换心,基本上主流的浏览器还是火狐,谷歌,IE,safrai这几种比较常见,所以在我们的开发中,有时候需要遇到判断用户正在使用什么浏览器以及 ...

  10. devenv 命令用法

    devenv是VisualStudio的可执行程序,一般安装在“C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”下. 这 ...