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 ...
随机推荐
- [ 原创 ] Java基础6--构造函数和抽象类的性质
构造函数的性质 // A.方法名与类名相同: // B.没有返回类型(例如return.void等):// C.不能被static.final.native.abstract和synchronized ...
- UVA1378 A funny stone game
博弈论. 就是有一堆石子你拿走一堆中的一个,然后再向后面两堆中加两个问胜负 i<j<=k 所以我们可以直接通过sg函数计算,考虑问题的奇偶性,如果这一位是奇的我们才考虑,偶的可以模仿 然后 ...
- POI2018
[BZOJ5099][POI2018]Pionek(极角排序+two pointers) 几个不会严谨证明的结论: 1.将所有向量按极角排序,则答案集合一定是连续的一段. 当答案方向确定时,则一个向量 ...
- UOJ 310 黎明前的巧克力(FWT)
[题目链接] http://uoj.ac/problem/310 [题目大意] 给出一个数集,A从中选择一些数,B从中选择一些数,不能同时不选 要求两者选择的数异或和为0,问方案数 [题解] 题目等价 ...
- 【漏洞预警】Intel爆CPU设计问题,导致win和Linux内核重设计(附测试poc)
目前研究人员正抓紧检查 Linux 内核的安全问题,与此同时,微软也预计将在本月补丁日公开介绍 Windows 操作系统的相关变更. 而 Linux 和 Windows 系统的这些更新势必会对 Int ...
- 20162303石亚鑫 第十二周hash补充博客
要求 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学号相加 ...
- hdu 1116 并查集判断欧拉回路通路
判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...
- java多线程技术之八(锁机制)
Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我 ...
- 【洛谷】4180:【模板】严格次小生成树[BJWC2010]【链剖】【线段树维护最大、严格次大值】
P4180 [模板]严格次小生成树[BJWC2010] 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说, ...
- poj 1466 Girls and Boys 二分图的最大匹配
Girls and Boys Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...