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

思路:

难,知道用分治算法,却不知道怎么用。只好看答案。

基本的思路是如果中位数是第K个数,A[i]如果是中位数,那么A[i]已经大于了i个数,还应大于K - i - 1个数 与B[K-i-2]对比。但是如果中位数不在A中我脑子就晕晕的。下面是大神代码,我还是没有看懂。

class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
// the following call is to make sure len(A) <= len(B).
// yes, it calls itself, but at most once, shouldn't be
// consider a recursive solution
if (m > n)
return findMedianSortedArrays(B, n, A, m); double ans = ; // now, do binary search
int k = (n + m - ) / ;
int l = , r = min(k, m); // r is n, NOT n-1, this is important!!
while (l < r) {
int midA = (l + r) / ;
int midB = k - midA;
if (A[midA] < B[midB])
l = midA + ;
else
r = midA;
} // after binary search, we almost get the median because it must be between
// these 4 numbers: A[l-1], A[l], B[k-l], and B[k-l+1] // if (n+m) is odd, the median is the larger one between A[l-1] and B[k-l].
// and there are some corner cases we need to take care of.
int a = max(l > ? A[l - ] : -(<<), k - l >= ? B[k - l] : -(<<));
if (((n + m) & ) == )
return (double) a; // if (n+m) is even, the median can be calculated by
// median = (max(A[l-1], B[k-l]) + min(A[l], B[k-l+1]) / 2.0
// also, there are some corner cases to take care of.
int b = min(l < m ? A[l] : (<<), k - l + < n ? B[k - l + ] : (<<));
return (a + b) / 2.0;
}
};

【leetcode】Median of Two Sorted Arrays(hard)★!!的更多相关文章

  1. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  2. 【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 s ...

  3. 【leetcode】Remove Duplicates from Sorted List (easy)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  4. leetcode 之Median of Two Sorted Arrays(五)

    找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...

  5. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  6. 【LeetCode】853. Car Fleet 解题报告(Python)

    [LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  7. 【LeetCode】390. Elimination Game 解题报告(Python)

    [LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimina ...

  8. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  9. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

随机推荐

  1. 使用struct实现面向对象编程的封装

    虽然C是面向过程的语言,但是这不代表C不能使用面向对象的思想,本质上说语言只是一种手段而已,一种外在的表现形式,支持面向对象的语言只是通过设计的特定的关键字更好的表现了面向对象编程而已.C中也可以使用 ...

  2. Fedora 20 创建桌面快捷方式

    创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...

  3. JavaIO和JavaNIO

    BIO和NIO BIO在之前的服务器处理模型中,在调用ServerSocket.accept()方法时,会一直阻塞到有客户端连接才会返回,每个客户端连接过来后,服务端都会accept一个新连接,接着启 ...

  4. How to force to Fullscreen Form

    Is it possibile by code resize a form to fullscreen? (like button Maximize) ? // VAR Changed on 10 J ...

  5. Java的哪些事

    Java的哪些事--------------------------------------------------Java学习分2个方面: Java语法与Java类库 Java: A simple, ...

  6. NodeJS下访问SQL Server

    1.下载node-sqlserver (1)msnodesql (msnodesql-0.2.1-v0.8-x64.msi)下载地址:下载  自行选择与自己系统相符的版本,点击安装. (2)msnod ...

  7. C语言基础:两个变量交换值的方法

    学习任何语言基础时,两个数值得交换是必须掌握的,下面是3种不同的方式(c语言) 方法一:利用数学的计算技巧 #include <stdio.h> int main() { , b = ; ...

  8. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  9. [转载+原创]Emgu CV on C# (三) —— Emgu CV on 均衡化

    本文简要描述了均衡化原理及数学实现等理论问题,最终利用emgucv实现图像的灰度均衡. 直方图的均衡化,这是图像增强的常用方法. 一.均衡化原理及数学实现(转载) 均衡化原理及数学实现可重点参看——& ...

  10. Eclipse配置默认的编码集为utf-8

    既然开了博,那就来点有用的. 可以使用下面的方法,让Eclipse对所有的项目里所有文件都按照指定的编码解析. Eclipse安装目录下有一个eclipse.ini文件, 用记事本打开即可,在最后一行 ...