找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值。

参考下面的思路,非常的清晰:

代码:

 double findMedianofTwoSortArrays(int A[], int B[], int m, int n)
{
int total = m + n;
//判断序列长度的奇偶,奇数只有一个中间值,偶数有两个中间值,取平均
if (total & 0x1)
return find_kth(A, m, B, n, total / + );
else
return (find_kth(A, m, B, n, total / ) + find_kth(A, m, B, n, total / + )) / ;
} int find_kth(int A[], int m, int B[], int n, int k)
{
//设定m<=n
if (m > n)return find_kth(B, n, A, m, k);
if (m == )return B[k - ];
if (k == )return min(A[], B[]); //删除掉一部分数据
int ia = min(k / , m), ib = k - ia;
if (A[ia - ] < B[ib - ])
return find_kth(A + ia, m - ia, B, n, k - ia);
else if (A[ia - ]>B[ib - ])
return find_kth(A, m, B + ib, m - ib, k - ib);
else
return A[ia - ];
}

leetcode 之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 (两个数组的mid值)

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

  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 & 归并排序

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

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

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

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

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

  9. leetcode 4. Median of Two Sorted Arrays

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

  10. leetcode之 median of two sorted arrays

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

随机推荐

  1. Docker学习笔记一:如何在线安装

    一.Docker简介: Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后 ...

  2. Function.caller, arguments.caller, arguments.callee, arguments.callee.calller

    Function.caller指向当前函数的调用者,是arguments.caller的替代者 arguments.caller也是指向当前函数的调用者,已被废弃 arguments.callee是对 ...

  3. oracle 插入每年每天数据

    create or replace procedure PROC_P_ABC is v_sumday ; i ; v_calendar_date number :=null; v_day number ...

  4. 把一个文件中所有文件名或者文件路径读取到一个txt文件,然后在matlab中读取

    链接: http://blog.csdn.net/dreamgchuan/article/details/51113295 dir /on/b/s  这个读取的是这样的格式:

  5. vim正则表达式小结

    http://note.youdao.com/noteshare?id=7ca2ac5d2f37fcb0e7a2a9c811c6e568

  6. 效应量Effect Size

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  7. Dozer 对象的"搬运工"

    前言:项目中,经常会遇到各层对象之间相互进行值传递的过程,如在数据据持久层有一持久类EntityA,在视图层可能会变为ViewA,通常情况下,这两个类的属性 名称 .类型都是一致的,   在两个对象传 ...

  8. 在Centos系统下使用命令安装gnome图形界面程序

    第一步:先检查yum 是否安装了,以及网络是否有网络.如果这两者都没有,先解决网络,在解决yum的安装. 第二步:在命令行下 输入下面的命令来安装Gnome包. # yum groupinstall ...

  9. .net中的lock

     lock锁 //定义一个私有成员变量,用于Lock的锁定标志 private static object lockobj = new object(); void DoSomething() { l ...

  10. 【LibreOJ】#6257. 「CodePlus 2017 12 月赛」可做题2

    [题意]数列满足an=an-1+an-2,n>=3.现在a1=i,a2=[l,r],要求满足ak%p=m的整数a2有多少个.10^18. [算法]数论(扩欧)+矩阵快速幂 [题解]定义fib(i ...