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

解题思路:

该题可以解决所有求有序数组A和B有序合并之后第k小的数!

该题的重要结论:

如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明。

具体的分析过程可以参考http://blog.csdn.net/zxzxy1988/article/details/8587244

class Solution {
public:
double findKth(int A[], int m, int B[], int n, int k)
{
//m is equal or smaller than n
if (m > n)
return findKth(B, n, A, m, k);
if (m == )
return B[k-];
if (k <= )
return min(A[], B[]); int pa = min(k / , m), pb = k - pa;
if (A[pa-] < B[pb-])
{
return findKth(A + pa, m - pa, B, n, k - pa);
}
else if(A[pa-] > B[pb-])
{
return findKth(A, m, B + pb, n - pb, k - pb);
} else
return A[pa-];
} double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int k = m + n;
if (k & 0x1)
{
return findKth(A, m, B, n, k / + );
} else
{
return (findKth(A, m, B, n, k / ) + findKth(A, m, B, n, k / + )) / ;
}
}
};

Median of Two Sorted Arrays-----LeetCode的更多相关文章

  1. Median of Two Sorted Arrays LeetCode Java

    两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...

  2. 3.Median of Two Sorted Arrays Leetcode

    难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...

  3. 4. Median of Two Sorted Arrays(topK-logk)

    4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...

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

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

  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(3) || Median of Two Sorted Arrays

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

  7. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

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

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

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

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

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

随机推荐

  1. jquery火焰等效果导航菜单

    jQuery-火焰灯效果导航菜单 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpre ...

  2. HDU 5371 Hotaru's problem (Manacher,回文串)

    题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求[前]和[中]是回文,[中]和[后]是回文.求3段最长为多少?由于平分的关系,所以答案应该是3的倍数. 思路:先Mana ...

  3. 20160123.CCPP详解体系(0002天)

    程序片段(01):字符.c 内容概要: 转义字符 #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdi ...

  4. 加密app.config

    EncryptConnection.EncryptConnectionString(true); public static class EncryptConnection { public stat ...

  5. 所有 HTTP 状态代码及其定义

    所有 HTTP 状态代码及其定义. 代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理,但处理尚未完成.  20 ...

  6. check windows return character

    #ifndef _FILE_CHECK_H#define _FILE_CHECK_H#include <string.h>#include <vector> const int ...

  7. C#的Timer

    PowerCoder 原文 C#的Timer 再C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Tim ...

  8. elasticsearch-head 的搭建

    elasticsearch-head 全部是js和html5写的,elasticsearch 全部都是http的接口, 这样,只需要简单地本地配置一个虚拟站点,就可以搭建  elasticsearch ...

  9. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_from_list(self, locator, *items)

    def select_from_list(self, locator, *items): """Selects `*items` from list identified ...

  10. php 采集程序 宋正河

    本程序主要是通过php采集网页信息,程序自动存储采集进度,采用phpquery简化元素节点匹配,采集到的内容直接入库 你要做的只是写好采集语法,本程序适合有一定php基础的人使用!当然你也可以进行修改 ...