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

public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
int k = A.length + B.length;
return k % 2 == 0 ? (findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2 + 1) +
findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2)) / 2
: findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2 + 1);
}
//返回两个数组中第k大的元素。
public double findK(int a[], int s1, int e1, int b[], int s2, int e2, int k) {
int m = e1 - s1 + 1;
int n = e2 - s2 + 1;
if (m > n) return findK(b, s2, e2, a, s1, e1, k); //a的长度比b的小。 if (s1 > e1) return b[s2 + k - 1];
if (s2 > e2) return a[s1 + k - 1];
if (k == 1) return Math.min(a[s1], b[s2]);
int midA = Math.min(k/2, m), midB = k - midA;
//假设a的第midA大的元素比b的第midB大的元素小,
//那么删掉a的前midA个元素,在剩余的数中找第k-midA大的。
if (a[s1 + midA - 1] < b[s2 + midB - 1])
return findK(a, s1 + midA, e1, b, s2, e2, k - midA);
else if (a[s1 + midA - 1] > b[s2 + midB - 1])
return findK(a, s1, e1, b, s2 + midB, e2, k - midB);
else
return a[s1 + midA - 1];
}
}

Leetcode: Median of Two Sorted Arrays. java.的更多相关文章

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

  2. [LeetCode] 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 ...

  3. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  4. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

  5. 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 sorted ...

  6. C++ Leetcode Median of Two Sorted Arrays

    坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...

  7. LeetCode——Median of Two Sorted Arrays

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  8. Median of Two Sorted Arrays(Java)

    求2个数组的中位数 方法很多 但是时间复杂度各异 1利用数组copy方法先融合两个数组,然后排序,找出中位数 import java.lang.reflect.Array; import java.u ...

  9. leetcode:Median of Two Sorted Arrays分析和实现

    这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...

随机推荐

  1. java 利用java运行时的方法得到当前屏幕截图的方法(转)

    将截屏图片保存到本地路径: package com.test; import java.awt.AWTException; import java.awt.Dimension; import java ...

  2. lua学习:使用Lua处理游戏数据

    在之前lua学习:lua作配置文件里,我们学会了用lua作配置文件. 其实lua在游戏开发中可以作为一个强大的保存.载入游戏数据的工具. 1.载入游戏数据 比如说,现在我有一份表单: data.xls ...

  3. os.path.exists(path) 和 os.path.lexists(path) 的区别

    使用os.path.exists()方法可以直接判断文件是否存在.代码如下:>>> import os>>> os.path.exists(r'C:\1.TXT') ...

  4. ThinkPHP - 自定义扩展类库

    首先在想要使用类库的地方建立文件夹,文件名称随意,不能使用class 然后在配置文件中: 'AUTOLOAD_NAMESPACE' => array( 'Lib' => './Lib', ...

  5. IF的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 编译kernel:内核makefile的作用

    < 嵌入式linux应用完全开发手册 > 韦东山 内核Makefile的使命: 编译哪些内核文件? 读取各级子目录makefile, .config, auto.conf, Kbuild, ...

  7. QQ与我联系

    第一种 <a href=" http://sighttp.qq.com/cgi-bin/check?sigkey=ee8bdb91c04a9ae912a305a5a2461a0d8d6 ...

  8. 06-C语言运算符2

    目录: 一.类型转换 二.条件运算符 三.逗号运算符 四.运算符优先级 回到顶部 一.类型转换 1.类型升级,浮点型高于整型,长整型高于整型,有符号与无符号取有符号. 2.在C语言中,类型转换没有提示 ...

  9. hdu3483之二项式展开+矩阵快速幂

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  10. Debian 7 下载

                                                                    Debian 7 DOWNLOAD http://cdimage.deb ...