题目

找两个排序数组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. 5.5 准备创建bean

    5.5  准备创建bean 我们不可能指望在一个函数中完成一个复杂的逻辑,而且我们跟踪了这么多Spring代码,经历了这么多函数,或多或少也发现了一些规律:一个真正干活的函数其实是以do开头的,比如d ...

  2. 团体程序设计天梯赛-练习集L1-016. 查验身份证

    L1-016. 查验身份证 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个合法的身份证号码由17位地区.日期编号和顺序编号 ...

  3. Cloud Insight 和 BearyChat 第一次合体,好紧张!

    说到 ChatOps 我们可能立刻想到是 Slack(啥?没听过?哦!),但是由于国内网络和语言的问题你可能无法拥有很好的体验了.那就把目光转回国内吧,国内的话就不得不提到 BearyChat 等 C ...

  4. tornado做简单socket服务器(TCP)

    http://blog.csdn.net/chenggong2dm/article/details/9041181 服务器端代码如下: #! /usr/bin/env python #coding=u ...

  5. Android ListView相关 头和尾 headView footerView

    ListView还可以添加头和尾部,而这头和尾就是View对象, 可以使用listView.addHeadView(view)方法和listView.addFootView(view)方法分别添加头和 ...

  6. 【疯狂Java讲义学习笔记】【数据类型与运算符】

    [学习笔记]1.8bit = 1byte,4byte = 1word.Java中的整型数据有byte(1字节),short(2字节),int(4字节),long(8字节).Java中的浮点数据有flo ...

  7. [itint5]单词变换

    http://www.itint5.com/oj/#42 基本上就是word ladder.直接来BFS,记录前驱. vector<string> transform(set<str ...

  8. QT UAC问题汇总贴

    http://www.qtcn.org/bbs/read-htm-tid-47983.html http://www.cnblogs.com/bombless/archive/2010/12/29/h ...

  9. MSSQLServer基础01(数据类型)

    数据库设计:范式 现阶段,必须遵守满足3NF 1范式:列的原子性,即列不可再拆分 2范式:表中不能描述多个信息,不能有数据冗余 3范式:引用其它表的主键信息 数据类型的意义: 1>提高效率.(减 ...

  10. 91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...