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. WAFの基本防护透明流模式v1.0

                      一.WAFの透明流模式     1)首先先配置WAF的网络,配置一个网桥接口,设置IP便于带内管理.             2)当然,如果需要不同网段之间都能够管 ...

  2. Java之反射 — 用法及原理

    Java之反射 - 用法及原理 定义 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象 ...

  3. Redis详解(三)——事务

    Redis详解(三)--事务 Redis事务的概念: Redis 事务的本质是一组命令的集合.事务支持一次执行多个命令,一个事务中所有命令都会被序列化.在事务执行过程,会按照顺序串行化执行队列中的命令 ...

  4. 八、Vue-lazyload

    一.Vue懒加载 文档:https://github.com/hilongjw/vue-lazyload 1.安装 cnpm i vue-lazyload -S 或 npm i vue-lazyloa ...

  5. C# MQTT M2MQTT

    MQTT 入门介绍 MQTT是基于二进制消息的发布/订阅编程模式的消息协议 实现MQTT协议需要客户端和服务器端通讯完成,在通讯过程中,MQTT协议中有三种身份:发布者(Publish).代理(Bro ...

  6. 小程序封装API

    一般我们https请求都是通过wx.request来请求,但是这种请求如果多了,页面会混乱不说,还不好管理,因此我将请求单独拎出去,方便管理,也方便后期维护. // api.js const API_ ...

  7. 修改tomcat的字符集问题

    转 默认情况下,tomcat使用的是iso8859-1的编码编码方式,浏览器的embed标签中src指向的地址要通过tomcat去解析.如果包含中文,采用这种编码方式就会出现乱码问题,而在这种情况下, ...

  8. TensorFlow--交互式使用--tf.InteractiveSession()

    用tf.Session()创建会话时只有在会话中run某个张量才能得到这个张量的运算结果,而交互式环境下如命令行.IPython,想要执行一行就得到结果,这就需要用到tf.InteractiveSes ...

  9. 147-PHP strip_tags函数,剥去字符串中的 HTML 标签(一)

    <?php $html=<<<HTM <title>PHP输出HTML代码</title> <body> <a href=#>转 ...

  10. jqGrid 添加 合计行 footDate

    jQuery(table_id).jqGrid({ url : url,//组件创建完成之后请求数据的url datatype : "json",//请求数据返回的类型.可选jso ...