中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素。

因此,我们考虑在一个任意的位置,将数组A划分成两部分。i表示划分数组A的位置,如果数组A包含m个元素,则划分位置有m+1种情况。因此,i的取值范围是0~m。

当i=0时,表示left_A为空;当i=m时,表示right_A为空。

同理,我们也可以划分B数组:

我们把left_A和left_B放到一个集合中,把right_A和right_B放到一个集合中。

如果想要获得中位数,要保证len(left_part)==len(right_part),并且max(left_part)<=min(right_part)。

因此,我们要寻找i,使其保证:

还要注意i=0,i=m,j=0,j=n的边界条件处理。

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
vector<int> s(nums1);
vector<int> l(nums2);
int m=nums1.size();
int n=nums2.size();
if(nums1.size()>nums2.size())
{
l=nums1;
s=nums2;
m=nums2.size();
n=nums1.size();
} int i,j;
int low=;
int high=m;
int num1,num2;
while(low<=high) //这里是对分割位置i进行二分搜索
{
i=(low+high)/;
j=(m+n+)/-i;
if(i> && j<n && s[i-]>l[j]) //i应当减小
high=i-;
else if(j> && i<m && l[j-]>s[i]) //i应当增大
low=i+;
else
{
if(i==)
num1=l[j-];
else if(j==)
num1=s[i-];
else
num1=max(s[i-],l[j-]);
break;
}
}
if((m+n)%==)
return num1;
if(i==m)
num2=l[j];
else if(j==n)
num2=s[i];
else
num2=min(s[i],l[j]);
return (num1+num2)/2.0;
} };

Median of Two Sorted 求两个有序数组的中位数的更多相关文章

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

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

  3. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

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

  5. 求两个有序数组的中位数或者第k小元素

    问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...

  6. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  7. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  8. 求两个有序数组的中位数(4. Median of Two Sorted Arrays)

    先吐槽一下,我好气啊,想了很久硬是没有做出来,题目要求的时间复杂度为O(log(m+n)),我猜到了要用二分法,但是没有想到点子上去.然后上网搜了一下答案,感觉好有罪恶感. 题目原型 正确的思路是:把 ...

  9. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

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

随机推荐

  1. PhpEclipse插件

    PHP开发工具 PHPEclipse PHPEclipse 是一个相当强大的一个Eclipse下开发PHP的插件,包括的功能有:PHP语法分析,调试,代码格式化,大纲视图,代码模板定制等. 安装地址: ...

  2. iphone 6s pp助手 越狱

    https://www.apple.com/iphone-6/合适初高中学习英语http://www.travelchinaguide.comhttp://jailbreak.25pp.com/ ip ...

  3. php 原理相关

    [PHP 运行方式(PHP SAPI介绍)] http://www.phpddt.com/php/php-sapi.html [PHP内核探索:从SAPI接口开始]http://www.nowamag ...

  4. Winpcap网络编程九之Winpcap实战,ARP协议获得MAC表及主机通信

    大家好,本次我们须要完毕的任务是: 完毕两台主机之间的数据通信(数据链路层) 仿真ARP协议获得网段内主机的MAC表 使用帧完毕两台主机的通信(Hello! I'm -) 声明:本文章的目的是为大家的 ...

  5. unity3d 捕获系统日志,来处理一些问题

    注册系统日志回调,根据日志内容和类型处理一些特殊问题 using UnityEngine; using System.Collections; public class SetupVerificati ...

  6. 利用Inotify和Rsync将webproject文件自己主动同步到多台应用server

    背景:须要搭建一套跟线上一模一样的环境,用来预公布,这是当中的web分发的一个小模块的实现过程. 1 工具以及环境简单介绍 1.1,Inotify工具 Inotify,它是一个内核用于通知用户空间程序 ...

  7. JS转换Decimal带千分号的字符串显示

    var numberChars = "0123456789"; /* Convert to decimal string */ function toDecimalString(v ...

  8. SQL判断是否存在符合某条件的记录

    IF EXISTS ( --判断是否存在合符条件的记录 ) FROM [DCL].[SecurityUser] WHERE [UserAccount] = @UserAccount ) BEGIN - ...

  9. canvas模糊事件处理

    不知道大家项目中有没有用到canvas时还有时候会出现模糊的情况: 具体推测可能是屏幕改变了,然而canvas的渲染对象并没有跟着一起变: 这里简单介绍个对象,window.devicePixelRa ...

  10. js判断是否安装flash

    <script type="text/javascript"> (function () { var noFlash = "你的浏览器没有安装Flash,会影 ...