题目:

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
10         return (double)(x+y)/2;
11     }
12 }
13  
14 public static int findKth(int[] A, int astart, int aend, int[] B, int bstart, int bend, int k) {
15     int m = aend - astart + 1;
16     int n = bend - bstart + 1;
17     
18     if(m>n)
19         return findKth(B,bstart,bend,A,astart,aend,k);
20     if(m==0)
21         return B[k-1];
22     if(k==1)
23         return Math.min(A[astart],B[bstart]);
24     
25     int partA = Math.min(k/2,m);
26     int partB = k - partA;
27     if(A[astart+partA-1] < B[bstart+partB-1])
28         return findKth(A,astart+partA,aend,B,bstart,bend,k-partA);
29     else if(A[astart+partA-1] > B[bstart+partB-1])
30         return findKth(A,astart,aend,B,bstart+partB,bend,k-partB);
31     else
32         return A[astart+partA-1];
33     }

Reference:

http://blog.csdn.net/yutianzuijin/article/details/11499917

http://blog.csdn.net/linhuanmars/article/details/19905515

Median of Two Sorted Array leetcode java相关推荐

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

  2. leetcode 81 Search in Rotated Sorted Array II ----- java

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  3. Merge k Sorted Lists leetcode java

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  4. Merge Two Sorted Lists leetcode java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  5. leetCode-数组:Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素 考察数组的基本操作: class Solution {public int removeDupli ...

  6. Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现

    Leetcode平台上的Median of Two Sorted Arrays题目,大意就是找两个已排序数组的中位数.今天先用快排的方式实现一下,代码如下: There are two sorted ...

  7. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

  8. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...

  9. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  10. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

最新文章

  1. 自定义Linq的Distinct
  2. 洛谷 P2359 三素数数
  3. cli3 px转rem适配移动端_Vue:将px转化为rem,适配移动端
  4. nasa数据库cm1数据集_获取下一个地理项目的NASA数据
  5. html中写色块,CSS3 彩虹色块
  6. 接口测试基础——第5篇xlrd模块
  7. 第三十一章 考试作弊
  8. docker build no such file or directory
  9. java 代码智能提示,如何在Eclipse中设置Java、JavaScript、HTML智能代码提示
  10. 车牌识别程序python代码_Python+Tensorflow+CNN实现车牌识别的示例代码
  11. ofo之死:一场商业“宫斗剧”下的祭品
  12. Windows下的CMake下载与安装
  13. cef 前进后台 实现_使用CefSharp前端后台交换
  14. 手机浏览器扫一扫的花样玩法,识万物还能答疑翻译
  15. DRM-Playready总结
  16. Segmentation简记5-AuxNet: Auxiliary tasks enhanced Semantic Segmentation for Automated Driving
  17. 2022-2028全球与中国健康资讯交换(HIE)市场现状及未来发展趋势
  18. 在家做什么小生意赚钱,这6种最适合在家操作!
  19. Mac ps 2021 3D功能无法使用问题,怎么办?
  20. mac m1 prometheus安装与启动

热门文章

  1. 土木工程与计算机专业考研学校排名,2017年土木工程专业考研大学排名
  2. struts2+spring的两种整合方式
  3. RedisRepository封装—Redis发布订阅以及StackExchange.Redis中的使用
  4. REST+EJB+JPA 框架在 Eclipse+TomEE 的开发环境搭建
  5. 包导出Android升级ADT22后会报ClassNotFoundException的原因分析
  6. 春晚魔术,醉翁之意不在酒
  7. 分类目录管理系统——软件开发项目实践
  8. 【SpringBoot_ANNOTATIONS】属性赋值 01 @Value赋值
  9. JAVA求n个数里最小的k个_n个数 找到最小的k个数 几种解法 和java实现
  10. php+mysql数据库语法错误_求教:PHP+MYSQL制作用户登录系统问题,总是提示数据库查询语句语法不对。$sql=mysql_query(。。。)这行...