4. Median of Two Sorted Arrays

给定两个有序的整数序列。求中位数,要求复杂度为对数级别。

通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别。但是代码实现较为困难。

换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分。划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R)

二分搜索这个划分即可。对于A+B的长度为奇数的情况,我们进行特殊处理,在划分时允许“借一位”。

其中一个序列为空则直接输出答案。

补充一个算法,对于两个无序的数列求中位数,《算法概论》中给出了线性的解法。通过类似快速排序的划分方法对数列进行划分,预测中位数可能存在的部分。

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums2.size()==0)
{
if(nums1.size()%2==0)return (double)(nums1[nums1.size()/2]+nums1[nums1.size()/2-1])/2.0;
else return (double)nums1[nums1.size()/2];
}
if(nums1.size()==0)
{
if(nums2.size()%2==0)return (double)(nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0;
else return (double)nums2[nums2.size()/2];
}
int len=(nums1.size()+nums2.size())/2;
bool flag=(nums1.size()+nums2.size())%2==1;
if(flag)len++;
int l=-1,r=min((int)nums1.size()-1,len-1);
while(true)
{
int i=(l+r)/2,ii;
ii=i+1;
int j=len-(i+1)-1,jj;
jj=j+1;
if(j>=(int)nums2.size()){l=i+1;continue;}
int l1=-2147483647,l2=-2147483647,r1=2147483647,r2=2147483647;
if(i>=0)l1=nums1[i];
if(j>=0)l2=nums2[j];
if(ii<nums1.size())r1=nums1[ii];
if(jj<nums2.size()) r2=nums2[jj];
if(flag&&l1>=l2)r1=min(l1,r1);
if(flag&&l2>l1)r2=min(r2,l2);
int maxa=max(l1,l2);int minb=min(r1,r2);
if(maxa<=minb){return (double)(maxa+minb)/2.0;}
if(l1>r2){r=i-1;continue;}
else{l=i+1;continue;}
} }
};

  

第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)的更多相关文章

  1. LeetCode(3) || Median of Two Sorted Arrays

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

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

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

  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(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

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

  8. [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

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

随机推荐

  1. N18_二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  2. ThinkPHP---thinkphp实用项

    [一]代码调试 (1)跟踪信息 ①简介:用于展示系统执行的相关状况,类似于快递的物流信息.ThinkPHP中默认关闭.如需使用,则通过配置项SHOW_PAGE_TRACE(显示页面跟踪)来配置. ②位 ...

  3. Sublime Text 3 快捷键(转载)

    本文转自:https://segmentfault.com/a/1190000002570753 (欢迎阅读原文,侵删) Sublime Text 3 快捷键精华版 Ctrl+Shift+P:打开命令 ...

  4. LINUX-挂载一个文件系统

    mount /dev/hda2 /mnt/hda2 挂载一个叫做hda2的盘 - 确定目录 '/ mnt/hda2' 已经存在 umount /dev/hda2 卸载一个叫做hda2的盘 - 先从挂载 ...

  5. Jmeter使用基础笔记-写一个http请求

    前言 本篇文章主要讲述2个部分: 搭建一个简单的测试环境 用Jmeter发送一个简单的http请求 搭建测试环境 编写flask代码(我参考了开源项目HttpRunner的测试服务器),将如下的代码保 ...

  6. PAT 1129 Recommendation System

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  7. 哈希表模板(Hash set)

    省选前最后的复(chui si)习(zheng zha). 上模板吧 namespace Hash_Table{ #define inf ~0U>>1 #define MaxN 10010 ...

  8. dev的动态汉化

    放控件TcxLocalizer.将其FIlename设定成汉化文件.ini.选择Locale的值是中文,然后active=true.OK了文件如下 ini如下: [2052] CHINA_STR=&q ...

  9. 关于jupyter notebook

    直接点击进行跳转阅读:https://zhuanlan.zhihu.com/p/33105153

  10. SQL 快速参考-----http://www.runoob.com/sql/sql-quickref.html

    http://www.runoob.com/sql/sql-quickref.html SQL 快速参考