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

因此,我们考虑在一个任意的位置,将数组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. C# Switch is Type

    常规用法: Type t = sender.GetType(); if (t == typeof(Button)) { var realObj = (Button)sender; // Do Some ...

  2. 什么是队列(Queue)?

    类似于链表和堆栈,队列也是存储数据的结构.队列中数据进入队列的顺序很重要,一般来说,队列就是一群人或者事物按照排好的顺序等待接受服务或者处理. 定义:队列,又称为伫列(queue),是先进先出(FIF ...

  3. Code Generation and T4 Text Templates

    Code Generation and T4 Text Templates Code Generation and T4 Text Templates

  4. 对中级Linux 用户非常有用的20 个命令

    也许你已经发现第一篇文章非常的有用,这篇文章是继对初级Linux用户非常有用的20个命令的一个延伸. 第一篇文章的目的是为新手准备的而这篇文章则是为了Linux的中高级用户.在这里你将学会如何进行自定 ...

  5. Unity 人物跟谁手指的移动(第一种方式)

    长夜漫漫无心睡眠,敲敲代码,越敲越来劲! 我发现好多小朋友都在玩熊出没之xxxx这个游戏,居然打了一下午都没玩通第2关,我把测试也叫来陪我一起玩! 结果他也打不通,我再去叫策划,他也没打过,我去叫主管 ...

  6. Unity Easy Save简单实用

    Easy Save使用: 1.保存游戏进度        2.设计游戏关卡(怪物数量,坐标,背景图等等) Easy Save默认存储地址: C:\Users\Administrator\AppData ...

  7. TS流PAT/PMT详解

    一 从TS流开始 从MPEG-2到DVB,看着看着突然就出现了一大堆表格,什么PAT.PMT.CAT……如此多的表该怎样深入了解呢? 我们知道,数字电视机顶盒接收到的是一段段的码流,我们称之为TS(T ...

  8. 关于String字符串反转

    这是网上看到的一篇java面试题中的问题: 问题是: 如何将一个String字符串反转. String str = "1234567"; int length = str.leng ...

  9. 论i++与++i

    网上看到好多人问i++与++i到底怎么理解,网友给出的答案几乎都是一样的.如下: i++:先进行计算,然后i自增1 ++i:i自增1,然后进行计算 并且课本上给出的解释跟这个也差不多,不过这样记起来既 ...

  10. Reverse Words in a String (JAVA)

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...