Median of Two Sorted Array leetcode java
题目:
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)).
题解:
首先我们先明确什么是median,即中位数。
引用Wikipedia对中位数的定义:
计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列。如果数据的个数是奇数,则中间那个数据就是这群数据的中位数;如果数据的个数是偶数,则中间那2个数据的算术平均值就是这群数据的中位数。
因此,在计算中位数Median时候,需要根据奇偶分类讨论。
解决此题的方法可以依照:寻找一个unioned sorted array中的第k大(从1开始数)的数。因而等价于寻找并判断两个sorted array中第k/2(从1开始数)大的数。
特殊化到求median,那么对于奇数来说,就是求第(m+n)/2+1(从1开始数)大的数。
而对于偶数来说,就是求第(m+n)/2大(从1开始数)和第(m+n)/2+1大(从1开始数)的数的算术平均值。
那么如何判断两个有序数组A,B中第k大的数呢?
我们需要判断A[k/2-1]和B[k/2-1]的大小。
如果A[k/2-1]==B[k/2-1],那么这个数就是两个数组中第k大的数。
如果A[k/2-1]<B[k/2-1], 那么说明A[0]到A[k/2-1]都不可能是第k大的数,所以需要舍弃这一半,继续从A[k/2]到A[A.length-1]继续找。当然,因为这里舍弃了A[0]到A[k/2-1]这k/2个数,那么第k大也就变成了,第k-k/2个大的数了。
如果 A[k/2-1]>B[k/2-1],就做之前对称的操作就好。
这样整个问题就迎刃而解了。
当然,边界条件页不能少,需要判断是否有一个数组长度为0,以及k==1时候的情况。
因为除法是向下取整,并且页为了方便起见,对每个数组的分半操作采取:
int partA = Math.min(k/2,m);
int partB = k - partA;
为了能保证上面的分半操作正确,需要保证A数组的长度小于B数组的长度。
同时,在返回结果时候,注意精度问题,返回double型的就好。
代码如下:
1 public static double findMedianSortedArrays(int A[], int B[]) {
2 int m = A.length;
3 int n = B.length;
4 int total = m+n;
5 if (total%2 != 0)
6 return (double) findKth(A, 0, m-1, B, 0, n-1, total/2+1);//k传得是第k个,index实则k-1
7 else {
8 double x = findKth(A, 0, m-1, B, 0, n-1, total/2);//k传得是第k个,index实则k-1
9 double y = findKth(A, 0, m-1, B, 0, n-1, total/2+1);//k传得是第k个,index实则k-1
return (double)(x+y)/2;
}
}
public static int findKth(int[] A, int astart, int aend, int[] B, int bstart, int bend, int k) {
int m = aend - astart + 1;
int n = bend - bstart + 1;
if(m>n)
return findKth(B,bstart,bend,A,astart,aend,k);
if(m==0)
return B[k-1];
if(k==1)
return Math.min(A[astart],B[bstart]);
int partA = Math.min(k/2,m);
int partB = k - partA;
if(A[astart+partA-1] < B[bstart+partB-1])
return findKth(A,astart+partA,aend,B,bstart,bend,k-partA);
else if(A[astart+partA-1] > B[bstart+partB-1])
return findKth(A,astart,aend,B,bstart+partB,bend,k-partB);
else
return A[astart+partA-1];
}
Reference:
http://blog.csdn.net/yutianzuijin/article/details/11499917
http://blog.csdn.net/linhuanmars/article/details/19905515
Median of Two Sorted Array leetcode java的更多相关文章
- Find Minimum in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Remove Duplicates From Sorted Array leetcode java
算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...
- Search in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- Find Minimum in Rotated Sorted Array leetcode
原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 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 t ...
随机推荐
- Python之路【第十篇】: python基础之socket编程
阅读目录 一 客户端/服务器架构 二 osi七层 三 socket层 四 socket是什么 五 套接字发展史及分类 六 套接字工作流程 七 基于TCP的套接字 八 基于UDP的套接字 九 recv与 ...
- html+css制作五环(代码极简)
五环 把五环做成一个浮动,总是位于屏幕中央的效果. 难点 定位的练习 position :fixed 位于body中间的时候 left:50%:top:50%; css内部样式表的使用 style=& ...
- CSU - 2056 a simple game
Description 这一天,小A和小B在玩一个游戏,他俩每人都有一个整数,然后两人轮流对他们的整数进行操作,每次在下列两个操作任选一个: (1)对整数进行翻转,如1234翻转成4321 ,1200 ...
- CodeForces - 831A Unimodal Array 模拟
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 【AI in 美团】深度学习在OCR中的应用
AI(人工智能)技术已经广泛应用于美团的众多业务,从美团App到大众点评App,从外卖到打车出行,从旅游到婚庆亲子,美团数百名最优秀的算法工程师正致力于将AI技术应用于搜索.推荐.广告.风控.智能调度 ...
- Android DecorView浅析
摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...
- luogu P3592 [POI2015]MYJ
题目链接 luogu P3592 [POI2015]MYJ 题解 区间dp 设f[l][r][k]表示区间l到r内最小值>=k的最大收益 枚举为k的位置p,那么包含p的区间答案全部是k 设h[i ...
- 如何成为一名优秀的CTO(首席技术官)
最近我发现很多开发人员都表示不知道如何规划职业生涯的下一个步骤.基于我们目前所处的科技泡沫现象,很多工程师都倾向于留在大型的成熟公司,或者要么a)去初创企业工作要么b)自己搞初创公司. 回顾我自己的职 ...
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
- Codeforces Round #298 (Div. 2) C. Polycarpus' Dice 数学
C. Polycarpus' Dice Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/p ...