找两个排好序的数组的中间值,实际上可以扩展为寻找第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. linux top命令查看系统资源

    参考材料: http://www.cnblogs.com/xd502djj/archive/2011/03/01/1968041.html

  2. Linux下chkconfig命令详解--(启动或停止)和查询系统服务的运行级信息

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...

  3. Python之旅:数字、字符串

    一 数字 整型与浮点型 #整型int 作用:年纪,等级,身份证号,qq号等整型数字相关 定义: age=10 #本质age=int(10) #浮点型float 作用:薪资,身高,体重,体质参数等浮点数 ...

  4. phpstorm 自定义属性没语法提示的问题

    在右侧Options勾选上Custom HTML tag attributes,并且在下面的输入框输入自定义属性列表,逗号分隔.

  5. windows使用composer.phar

    把composer.phar复制到php安装目录下, 新建一个composer.bat文件,编辑 @php "%~dp0composer.phar" %* 保存,然后就可以使用co ...

  6. python--文件操作删除某行

    使用continue跳过本次写循环就可以了 #文本内容 Yesterday when I was young 昨日当我年少轻狂 The tasting of life was sweet 生命的滋味是 ...

  7. kendalltau肯德尔和谐系数

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

  8. [Java多线程]-线程池的基本使用和部分源码解析(创建,执行原理)

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 多线 ...

  9. 【Web】Struts之namespace

    ZZ:struts2学习:配置篇之namespace 在struts1中是没有命名空间这个概念的,通过命名空间我们可以将所有的action配置划分为一个个逻辑单元,每个单元都有它自己的标识前缀.命名控 ...

  10. input美化问题

    对于前端来说,一个比较大的问题是,当HTML提供的最原始的控件长的很丑可是又没有相应的属性去更改,碰到这个问题时我们应该怎么办?比如radio的那个单选框我觉得好丑啊,怎么办呢?一个很好的思路就是把它 ...