4. Median of Two Sorted Arrays

官方的链接:4. Median of Two Sorted Arrays

Description :

There are two sorted arrays nums1 and nums2 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)).

Example1:


nums1 = [1, 3]

nums2 = [2]

The median is 2.0


Example2:


nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5


问题描述

给定长度分别为m和n的两个排序数组nums1和nums2,在时间复杂度O(log (m+n))内算出数组的中位数

思路

两个有序数组的中位数和Top K问题类似。这里从小到大直接定位第k个,简单理解为从nums1中获取第i个,而从nums2中获取第j=k-i个,其中i=0~k。当定位到nums[i-1]<=nums2[j]和nums[j-1]<=nums[i]即完成,当然还有边界问题。

把中位数也当作是top k问题,最后进行奇偶判断。详情可参考这里Share my O(log(min(m,n)) solution with explanation,代码也是借鉴的。

值得注意的是几个边界判断问题,比如i为0或者m,j为0或者n。

[github-here]

 public class Q4_MedianOfTwoSortedArrays {
public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length;
int n = nums2.length;
// make sure that m <= n
if (m > n) {
return findMedianSortedArrays(nums2, nums1);
}
// n>=m,i = 0 ~ m,so j = (m + n + 1) / 2 -i > 0.
int i = 0, j = 0, imax = m, imin = 0, halfLen = (m + n + 1) / 2;
int maxLeft = 0, minRight = 0;
while (imin <= imax) {
i = (imin + imax) / 2;
j = halfLen - i;
if (i < m && nums2[j - 1] > nums1[i]) {
imin = i + 1;
} else if (i > 0 && nums1[i - 1] > nums2[j]) {
imax = i - 1;
} else {
if (i == 0) {
//the target is in nums2
maxLeft = nums2[j - 1];
} else if (j == 0) {
//the target is in nums1
maxLeft = nums1[i - 1];
} else {
maxLeft = Math.max(nums1[i - 1], nums2[j - 1]);
}
break;
}
}
//odd
if ((m + n) % 2 == 1) {
return (double)maxLeft;
}
//even
if (i == m) {
//nums1 is out of index m
minRight = nums2[j];
} else if (j == n) {
//nums2 is out of index n
minRight = nums1[i];
} else {
//others
minRight = Math.min(nums1[i], nums2[j]);
}
return (double) (maxLeft + minRight) / 2;
} public static void main(String[] args) {
new Q4_MedianOfTwoSortedArrays().findMedianSortedArrays(new int[] { 1, 3 }, new int[] { 2 }); }
}

Q4:Median of Two Sorted Arrays的更多相关文章

  1. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  2. LeetCode2: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 sor ...

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

  4. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

  5. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  6. 4:Median of Two Sorted Arrays

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

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. leetcode 4:Median of Two Sorted Arrays

    public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...

  9. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. 007.Delphi插件之QPlugins,插件的卸载和重新加载

    效果图如下,可以反复卸载和重新加载.QPlugins这个插件,还没弄明白,摸索着跟着DEMO写 主窗口代码如下 unit Frm_Main; interface uses Winapi.Windows ...

  2. .net core文件系统简介

    在asp.net core程序中,我们可以通过如下代码开启对Web 根目录内的文件静态访问功能: app.UseStaticFiles(); 如果要提供更高级的选项,例如:将其它的物理文件夹下的文件作 ...

  3. CSU 1216 异或最大值

    求n个数里面,求两两异或的最大值 直接来肯定会超时 既然要异或最大值,那么两个数的二进制肯定是正好错开为好...为了能快速找到错开的数,确实有点难想到,用字典树,按二进制数插入,再一个一个在字典树里面 ...

  4. leetcode102 Binary Tree Level Order Traversal

    """ Given a binary tree, return the level order traversal of its nodes' values. (ie, ...

  5. Ubuntu18.04 LTS 搭建Cassandra集群

    环境需求 jdk8 root@node01:~# java -version java version "1.8.0_202" Java(TM) SE Runtime Enviro ...

  6. jmeter简单压测、下载文件

    一.jmeter做简单压测(单机) 1.添加需要压测的HTTP请求 2.添加聚合报告 3.设置压测场景 4.查看聚合报告 二.多机同时进行压测 1.在需要连接的电脑上打开jmeter  bin目录下的 ...

  7. Docker自建本地仓库

    1.安装docker yum install docker -y 之后根据需求把指定docker安装目录,或者新建一个磁盘使用docker-storage-setup来使用 2.设置第三方docker ...

  8. Emacs: 设置窗口标题格式

    Emacs默认的窗口标题常常比较简单,例如我的默认标题是"emacs@ubuntu".如果想要在标题中显示更多的信息,我们可以对它进行一些设置.由于Emacs的可扩展性,这种修改实 ...

  9. 138-PHP static后期静态绑定(一)

    <?php class test{ //创建test类 public function __construct(){ self::getinfo(); //后期静态绑定 } public sta ...

  10. ServletConfig详解

    ServletConfig是Servlet中的init()方法的参数类型,服务器会在调用init()方法时传递ServletConfig对象给init()方法.   ServletConfig对象封装 ...