题目

找两个排序数组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. Ext4.2 grid 条件查询使用

    项目中用到Ext4.2,初次接触,用的不是太熟,做个总结,恳请指正! 1.grid重新设置条件,查询结果不是从第1页开始 在处理grid条件查询时,点击搜索按钮调用store.load()方法时,会把 ...

  2. ExtJS4.2学习(11)可拖放的表格(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-18/180.html --------------- ...

  3. js中replace的正则替换

    temp: video":"\t<ul class=\"g-list tabview-cont on\">\t\r\n\t\t<li clas ...

  4. hdu 1085

    额    母函数 #include <cstdio> #include <cstring> int a[3],b[3]= {1,2,5}; int c1[10001],c2[1 ...

  5. HDU 1429 胜利大逃亡(续)(三维BFS)

    题目链接 题意 : 中文题不详述. 思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接 #include <stdio.h> #include <stri ...

  6. 在linux下,查看一个运行中的程序, 占用了多少内存

    1. 在linux下,查看一个运行中的程序, 占用了多少内存, 一般的命令有 (1). ps aux: 其中  VSZ(或VSS)列 表示,程序占用了多少虚拟内存. RSS列 表示, 程序占用了多少物 ...

  7. request重定向或者是response转发请求后面的代码依然执行

    调用response.redirect(),或者request.getRequestDispatcher(loginAddr).forward(request,response);后,后面的代码照样执 ...

  8. android 内部存储相关知识点: getfilestreampath getDir 子文件夹

    文件系统的API的命名方式和常规的不一样: 都是get命名,但是功能就是能创建文件夹...   这种方式的API 命名习惯和常规的不一样...  createXXX ----方便查找 http://i ...

  9. 非常好的Demo网站

    http://www.xdemo.org/

  10. HTTP/1.1 Range和Content-Range

    http://www.cnblogs.com/bayonetxxx/archive/2011/03/19/1989162.html 假设你要开发一个多线程下载工具,你会自然的想到把文件分割成多个部分, ...