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

因此,我们考虑在一个任意的位置,将数组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. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  2. Permutation Sequence 解答

    Question The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...

  3. HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. hibernate多对一的操作解析

    在hibernate的关联操作中有很多关系,其中多对一关系是最常见的.我们看这两个表. 这里有部门表和员工表. 那么我们可以这么说一个部门可以有多个员工.这就是1对多的关系.这是我们站在部门表的角度上 ...

  5. iOS SDK原生JSON解析

    - (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...

  6. 让你的javascript函数拥有记忆功能,降低全局变量的使用

    考虑例如以下场景:假如我们须要在界面上画一个圆,初始的时候界面是空白的.当鼠标移动的时候,圆须要尾随鼠标移动.鼠标的当前位置就是圆心.我们的实现方案是:假设界面上还没有画圆,那么就新创建一个:假设已经 ...

  7. 基于Vue 和 webpack的项目实现

    Vue.js 是一款极简的 mvvm 框架,如果让我用一个词来形容它,就是 “轻·巧” .如果用一句话来描述它,它能够集众多优秀逐流的前端框架之大成,但同时保持简单易用.废话不多说,来看几个例子: & ...

  8. 使用有限状态机(FSM)编写的敌人AI

    using UnityEngine; using System.Collections; public class AttackState : FSMState { public AttackStat ...

  9. HDU 1088 - Write a simple HTML Browser

    直接看sample input = = 又一道模拟. #include <iostream> #include <string> #include <cstdio> ...

  10. Linux程序设计 读笔3 文件操作

    一 linux文件结构 二 系统调用和设备驱动程序 三 库函数 四 底层文件访问 五 标准IO库 六 格式化输入输出 七 文件和目录的维护 八 扫描目录 九 错误处理 十