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 ...
随机推荐
- select组件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 【原】Shell脚本-判断文件有无进而复制
2016年7月5日某同学在群上求助要编一个判断文件或目录在某路径下有无进而有的就复制粘贴到另一路径下,无的则将代码中断(不往下执行命令)的脚本.逐一完善.模板如下(生产环境可用到路径环境变量) --- ...
- Nginx高性能服务器安装、配置、运维 (1) —— Nginx简介
一.Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器. Nginx特点 ...
- (转)CSS行高——line-height
原文地址:http://www.cnblogs.com/dolphinX/p/3236686.html 初入前端的时候觉得CSS知道display.position.float就可以在布局上游刃有余了 ...
- 隐藏/显示 我的电脑盘符驱动…
组策略里更改即可:点击"开始"→"运行",输入"gpedit.msc",打开组策略.在窗口左侧的"本地计算机策略"中依次 ...
- postgresql行转列并拼接字符串
有这样一张表: ; id | kw ----+-------- 1 | big 1 | hello 2 | oracle 2 | small 2 | apple 3 | shit( ...
- Content by query webpart 自定义样式的使用方法
今天研究一个非常实用的webpart 如果在office365 上 的webpart中一直没“内容查询”, 这里需要开启2个features:http://community.office365.co ...
- 常用JS正则表达式收集
1.去掉字符串前后空格,不会修改原有字符串,返回新串.str.replace(/(^\s*)|(\s*$)/g,'');
- 一些简单的帮助类(1)-- String的类型验证
在工作中经常会遇到 验证String 中的值是否属于Int型或者是Bool又或是Date 一般的做法是用方法 类型.TryParse(string,类型) 来做验证. "; int intV ...
- spring mvc 笔记
springmvc 课堂笔记 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想, ...