题目

找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n))。

解法

更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值。

代码

 class Solution
{
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int total = m + n;
if(total & 0x1) //总数为奇数,返回中位数
return (double)findKthSortedArrays(A, m, B, n, total/ + );
else //总数为偶数,返回中间两个数的平局值
return (findKthSortedArrays(A, m, B, n, total/) +
findKthSortedArrays(A, m, B, n, total/ + )) / 2.0;
} private:
int findKthSortedArrays(int A[], int m, int B[], int n, int k)
{
if( m > n ) //保证第一个数组比第二个短
return findKthSortedArrays(B, n, A, m, k);
if( m == ) //短数组为空
return B[k - ];
if( k == ) //求第1个元素
return min(A[], B[]); int ia = min(k/, m), ib = k - ia; //比较各自的第k/2个元素,如果短数组长度小于k/2则用最后一个元素来做比较,B数组也相应调整比较的元素
if( A[ia - ] > B[ib - ] ) //短数组的大,那个短数组的右边部分必然排在第k之后,同理长数组左边的部分必然都在第k之前
return findKthSortedArrays(A, ia, B + ib, n - ib, k - ib);
else if( A[ia - ] < B[ib - ] )
return findKthSortedArrays(A + ia, m - ia, B, ib, k - ia);
else
return A[ia - ]; //如果相等则找到
}
};

LeetCode题解——Median of Two Sorted Arrays的更多相关文章

  1. LeetCode题解-----Median of Two Sorted Arrays

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

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

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

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

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

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

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

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

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

  6. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  7. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  8. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  9. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

随机推荐

  1. Oracle数据库-建库、建表空间,建用户

    Oracle安装完后,其中有一个缺省的数据库,除了这个缺省的数据库外,我们还可以创建自己的数据库. 对于初学者来说,为了避免麻烦,可以用'Database Configuration Assistan ...

  2. 弹窗开关js

    // var guanbi = false; // $("#testbtn").click(function(){ // if(guanbi){ // $("#tan&q ...

  3. hdu 4403

    水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...

  4. "Principles of Reactive Programming" 之<Actors are Distributed> (3)

    Cluster 讲课的这哥们接下来讲了下Akka Cluster的使用,但是是通过把一个以前讲过的actor 系统改成使用cluster来介绍的Akka cluster. 这部分代码很多,还是直接看视 ...

  5. Samza文档翻译 : Comparison Introduction

    http://samza.incubator.apache.org/learn/documentation/0.7.0/comparisons/introduction.html 这里有一些使得Sam ...

  6. c缺陷与陷阱笔记-第一章 词法陷阱

    1.运算符的贪心性,匹配最长的运算符,例如 n-->0,从-开始,-是运算符,--是运算符,-->就不是,所以是 n -- > 0,--是 a---b,-是,--是,,---不是,所 ...

  7. linux 2.6 互斥锁的实现-源码分析

    http://blog.csdn.net/tq02h2a/article/details/4317211 看了看linux 2.6 kernel的源码,下面结合代码来分析一下在X86体系结构下,互斥锁 ...

  8. linux下使用yum安装mysql、tomcat、httpd

    一.linux下使用yum安装mysql   1.安装 查看有没有安装过:           yum list installed mysql*           rpm -qa | grep m ...

  9. 生成整数自增ID(集群主键生成服务)

        在集群的环境中,有这种场景     需要整数自增ID,这个整数要求一直自增,并且需要保证唯一性. Web服务器集群调用这个整数生成服务,然后根据各种规则,插入指定的数据库.        一般 ...

  10. BIRT使用1:简介、概念、元素、报表设计器组成

    前一篇博客对birt进行了一个初探,相信通过上篇博客大家对birt有个初步认识,接下来我们随着下面这张思维导图的展示,进入birt的使用学习. 这一篇博客是第一部分,主要介绍一下birt的简介.概念. ...