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

Tags: Divide and Conquer, Array, Binary Search

分析:

对于数字个数k,如果k为奇数,则k个数的中位数为第(k/2+1)个;对于偶数,中位数为第k/2和(k/2+1)的平均数。

而对于两个有序数组a[m] b[n],找其第i个数的方法是:第一个数组取出前i/2个数,第二个数组则取出前(i-i/2)个数(注意不一定等于i/2),然后比较各自最大的数,如果a[i/2-1]<b[i-i/2-1],则a数组的前i/2个数全是第i个数之前的数,然后从a数组剩下(m-i/2)个数的子数组和b数组中继续取第(i-i/2)个数。

当然,这里可能会出现m<i/2的情形,这时a就取出其前m个数,b取出前i-m个数即可。对称的情况也可能出现在b上面,这时只需要更换下参数顺序即可。

最后说下终止条件,当m全取完即m==0时,就返回b[i-1]。当i/2-1<0即i<2时,无法继续细分(因为细分后要比较a[i/2-1]和b[i-i/2-1]),说明只需要从两个数组中取出第一个,则返回min(a[0],b[0])即可。

c++代码:

 class Solution {
public:
double findIth(int A[], int m, int B[], int n, int i) {
if (m > n)
return findIth(B, n, A, m, i);
if (m == ) {
return B[i - ];
}
if (i <= ) {
return min(A[], B[]);
}
int aSeg = min(m, i / ), bSeg = i - aSeg;
if (A[aSeg - ] < B[bSeg - ]) {
return findIth(A + aSeg, m - aSeg, B, n, i - aSeg);
} else if (A[aSeg - ] > B[bSeg - ]) {
return findIth(A, m, B + bSeg, n - bSeg, i - bSeg);
} else {
return A[aSeg - ];
}
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int num = m + n;
if (num % == ) {
return findIth(A, m, B, n, num / + );
} else {
return (findIth(A, m, B, n, num / + )
+ findIth(A, m, B, n, num / )) / ;
}
}
};

LeetCode Algorithm 04_Median of Two Sorted Arrays的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

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

  4. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  5. LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard

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

  6. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  7. 【JAVA、C++】LeetCode 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 two ...

  8. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

  9. [LeetCode][Python]Median of Two Sorted Arrays

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...

随机推荐

  1. 线段树 hdu3642 Get The Treasury

    不得不说,这是一题很经典的体积并.. 然而还是debug了2个多小时... 首先思路:按z的大小排序. 然后相当于扫描面一样,,从体积的最下方向上方扫描,遇到这个面 就将相应的两条线增加到set中,或 ...

  2. vim 配置.vimrc文件

    下面这个.vimrc文件是根据公司里的一个前辈配置的,这里记录下,方便以后使用.它的功能,其实跟网上很多.vimrc配置的相比,还是小儿科.我记录下来,主要还是因为自己已经习惯了这个工作环境跟快捷键. ...

  3. Onvif开发之服务端成功对接Rtsp视频流篇

    前面篇介绍onvif服务端的发现功能,继续在之前的代码基础上完成一个RTSP流的工作,也就是客户端通过ONVIF协议来预览设备端在这个之前必须确定几个简单的条件1 设备端能被发现2 设备端支持RTSP ...

  4. C++标准库概述

    一.C++标准库的主要组件: 1.标准C库 2.I/O流技术(对标准输入输出设备称为标准I/O,对在外磁盘上文件的输入输出称为文件I/O,对内存中指定的字符串存储空间的输入输出称为串I/O) 3.st ...

  5. EF搭建数据库

    http://blog.csdn.net/mss359681091/article/details/52135867http://blog.csdn.net/x_craft/article/detai ...

  6. 搭建 Docker 环境

  7. artDialog提示框、对话框

    /** * 警告 * @param {String}消息内容 */ artDialog.alert = function (content, callback) { return artDialog( ...

  8. PHP GD 生成图片验证码+session获取储存验证码

    以下分享一个PHP制作的图片验证码案例:案比例如以下图: 运用PHP GD具体请看:http://www.php.net/manual/zh/book.image.php 后台图片代码例如以下: &l ...

  9. Linux 内存管理与系统架构设计

    Linux 提供各种模式(比如,消息队列),但是最著名的是 POSIX 共享内存(shmem,shared memory). Linux provides a variety of schemes ( ...

  10. 1.3 Quick Start中 Step 5: Start a consumer官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 5: Start a consumer Step : 消费消息 Kafka ...