题目

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

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5

答案

看到题目要求O(log (m+n)),知道应该用二分,但是没有想到具体实施的办法,在网上搜了答案,看懂了完成的代码,这道题刷新了我对二分的看法,神一样的存在。

代码

 #define MIN(a,b) ((a) > (b) ? (b) : (a))

 class Solution {
public:
int findMedNum(vector<int>::iterator nums1Beg,int nums1Num,vector<int>::iterator nums2Beg,int nums2Num,int med)
{
if(nums1Num == )
{
return *(nums2Beg + med - );
}
if(nums1Num > nums2Num)
{
return findMedNum(nums2Beg,nums2Num,nums1Beg,nums1Num,med);
} if(med == )
{
return MIN(*nums1Beg,*nums2Beg);
} int nums1BegMed = MIN(med / , nums1Num);
int nums2BegMed = med - nums1BegMed; if(*(nums1Beg + nums1BegMed - ) == *(nums2Beg + nums2BegMed - ))
{
return *(nums1Beg + nums1BegMed - );
} if(*(nums1Beg + nums1BegMed - ) < *(nums2Beg + nums2BegMed - ))
{
return findMedNum(nums1Beg + nums1BegMed,nums1Num - nums1BegMed,nums2Beg,nums2BegMed,med - nums1BegMed);
} if(*(nums1Beg + nums1BegMed - ) > *(nums2Beg + nums2BegMed - ))
{
return findMedNum(nums1Beg,nums1BegMed,nums2Beg + nums2BegMed,nums2Num - nums2BegMed,med - nums2BegMed);
} return ;
} double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums1.size() == && nums2.size() == )
{
return ;
}
int totalLen = nums1.size() + nums2.size(); if(totalLen & 0x1)
{
return findMedNum(nums1.begin(),nums1.size(),nums2.begin(),nums2.size(),(totalLen / ) + );
}
else
{
int pre = findMedNum(nums1.begin(),nums1.size(),nums2.begin(),nums2.size(),totalLen / );
int post = findMedNum(nums1.begin(),nums1.size(),nums2.begin(),nums2.size(),(totalLen / ) + );
return (double)(pre + post) / 2.0;
} return ;
}
};

leetcode-【hard】4. Median of Two Sorted Arrays的更多相关文章

  1. 【LeeetCode】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 ...

  2. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

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

  3. 【一天一道LeetCode】#4 Median of Two Sorted Arrays

    一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...

  4. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  5. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  6. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  7. 【LeetCode】4. Median of Two Sorted Arrays(思维)

    [题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...

  8. 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...

  9. 【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 t ...

  10. 【LeetCode】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 ...

随机推荐

  1. final发布评价

    1.飞天小女警: 礼物挑选这个项目相比之前的发布功能更完善了些,但是整体界面还是不太美观,界面上呈现出的选项字不够清晰,最为最终产品其功能还是少了点儿.在发布过程中整体表达比较清晰.流畅,比较不错. ...

  2. php mb_convert_encoding的使用

    mb_convert_encoding( $str, $encoding1,$encoding2 ): $str,要转换编码的字符串 $encoding1,目标编码,如utf-8,gbk,大小写均可 ...

  3. .net frameworkAPI文档下载地址

    http://www.msdn.hk/html/2014/5.html VS2013 ILdasm 反编译工具安装在下面地址里了 C:\Program Files (x86)\Microsoft SD ...

  4. javascript 键盘输入过滤,只能输入数字,小数一位且只能输入5

    $("#right_div2 input[type='text'][class='textClass'][id^='asd_']").live("keydown" ...

  5. 【转】深入 Docker:容器和镜像

    在本专栏往期的 Flux7 系列教程 里,我们已经简单地探讨了 Docker 的基本操作.而在那篇教程中,我们一直是简单地将容器当成是"正在运行的镜像",并没有深入地区分镜像和容器 ...

  6. linux 1-100的累加

    [   ]   判断式.它的使用和test命令一样 [ ]的判断符,只会返回2种值.0(真) 非0(假) -gt 大于-lt 小于-eq 等于-ne 不等于-ge 大于等于-le 小于等于 while ...

  7. 委托、回调 Lambda表达式书写方式

  8. Android开发中经常使用的Content-Type简介

    1.application/x-www-form-urlencoded:最常使用的类型(默认也是这种类型),主要用于提交不带文件的post数据. 2.multipart/form-data:需要结合b ...

  9. [转]C#程序无法在64位系统上运行之.NET编译的目标平台

    今天将编译的C#的exe拷贝到测试机上(Win7_64bit),一运行就挂了,提示“stop working”,一开始怀疑测试机上没有安装.net framework框架,追究半天原来是编译的目标平台 ...

  10. bzoj1039: [ZJOI2008]无序运动Movement

    Description D博士对物理有着深入的研究,经典物理.天体物理.量子物理都有着以他的名字命名的定理.最近D博士着迷于研究粒子运动的无规则性.对圣经深信不疑的他相信,上帝创造的任何事物必然是有序 ...