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

因此,我们考虑在一个任意的位置,将数组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. 2015第10周日CSS—3

    CSS各种居中方法 CSS的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中,不同的居中用不同方法. 水平居中 1.行内元素水平居中(文本,图片) 给父层设置 text-align ...

  2. Activiti 5.16用户手册

    From :http://www.mossle.com/docs/activiti/ Table of Contents 1. 简介 协议 下载 源码 必要的软件 JDK 6+ Eclipse Ind ...

  3. HOWTO Use Python in the web — Python v3.0.1 documentation

    HOWTO Use Python in the web - Python v3.0.1 documentation mod_python¶ People coming from PHP often f ...

  4. 一些常用运行命令和CMD命令

    运行命令 1. 进入服务页面的命令: services.msc 2. 远程连接命令:mstsc.exe 3. 配置电脑启动项 msconfig 4. 计算器 calc.exe 5. 设定关机时间(se ...

  5. Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation

    问题:Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation 1. Windows-& ...

  6. RMAN-format变量及configuration配置项

    一.FORMAT字符串替代变量使用FORMAT参数时可使用的各种替换变量,如下:%c:备份片的拷贝数(从1开始编号):%d:数据库名称:%D:位于该月中的天数 (DD):%M:位于该年中的月份 (MM ...

  7. MSDTC问题集

    一.链接服务器的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务. 尊重原著作:本文转载自http://sfwxw456.blog.163.com/blog/sta ...

  8. Intersection of Two Linked Lists(java)

    eg: a1 → a2 ↘ c1 → c2 → c3 或者直接a1 → b1 ↗ b1 → b2 → b3求公共链表c1. 常规的指针分裂法,复制法,偏移法. public class Solutio ...

  9. J - A + B Problem II(第二季水)

    Description I have a very simple problem for you. Given two integers A and B, your job is to calcula ...

  10. Spring中注解的使用详解

    一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...