Median of Two Sorted 求两个有序数组的中位数
中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素。
因此,我们考虑在一个任意的位置,将数组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 求两个有序数组的中位数的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 求两个有序数组的中位数或者第k小元素
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 求两个有序数组的中位数(4. Median of Two Sorted Arrays)
先吐槽一下,我好气啊,想了很久硬是没有做出来,题目要求的时间复杂度为O(log(m+n)),我猜到了要用二分法,但是没有想到点子上去.然后上网搜了一下答案,感觉好有罪恶感. 题目原型 正确的思路是:把 ...
- 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 ...
随机推荐
- LeeCode-Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 电子科大POJ "统计单词"
统计单词 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) C-sources: ...
- IOS 计算密码强度
+ (BOOL) judgeRange:(NSArray*)conditionArr Password:(NSString*)password { NSRange range; BOOL result ...
- Capture the Flag(模拟)
Capture the Flag Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In computer se ...
- 更改yum网易 阿里云的yum源。
一,鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源,改为国内的Yum源,著名的有网易 阿里云源.如何更改呢? 二,更改yum源为网易的. 首先备份/etc/yum.repos.d/Cent ...
- diffuse linux 文件比对工具
linux下比较好用的可视化文件比对工具
- 易语言转C#小试牛刀
呵呵,用了几年的易语言,太郁闷了,玩过E的童鞋们懂得,偶然机会尝试C#,现正式投入C#门下. 我会把我学习C#的一些知识和重点,实时发不到我的BLOG中,同想学习C#的童鞋一起成长起来.
- android——fragment长时间home或者锁屏java.lang.IllegalArgumentException:No view found for id for....
在项目中遇到了这个问题.头痛了很久,总是无法重现,也不能很好的解决.总是在息屏后一段时间,就会报java.lang.IllegalArgumentException:No view found for ...
- 读取webconfig里面的appSetting和connectionString
<appSettings> <add key="SiteURL" value="http://moss2007:7000" /> < ...
- JQ点击高亮显示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...