题目

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. CE STEPLDR

    作用:初始化CPU.内存.Flash,复制EBoot到内存并跳入EBoot中运行. 原理:S3C2416有 8-KB 的steppingstone(暂时翻译为垫脚石),在Nand启动模式下可把Nand ...

  2. Python 批量修改文件名

    最近下载了几部美剧(越狱.迷失.权利的游戏......),每集文件名都好长好长..想改短一些,但一个一个改太累了,于是写了个脚本来实现批量修改: 修改前文件名: 修改后文件名: 代码实现: #enco ...

  3. Static List

    Static ListStatic List is the smart implementation of list data structure for those languages that h ...

  4. div四个边框分别设置阴影样式

    对于div边框的阴影一直没有很好地理解,也一直不明白怎么给四个边框分别设置阴影.昨天项目中碰到了这个问题,就认真想了一下,在此总结一二. 首先,还是从官方解释说起. 网上的解释通常都是什么水平阴影长度 ...

  5. 图片垂直居中 和 float

    //图片垂直居中, display:table-cell; vertical-align:middle;   不能和 css (float)元素共存,可以在元素外面多加一个层 css .th-left ...

  6. spring常用注解

    使用注解构造IOC.替代传统的applicationContext.xml配置<bean/>和<property/> 传统的spring要在applicationContext ...

  7. netflix:Conductor微服务编排引擎

    项目地址: https://github.com/Netflix/conductor Conductor 是 Netflix 受需要运行全球流媒体业务流程的启发,构建的基于云的微服务编排引擎. Con ...

  8. [2015.07.27]万峰图片批量处理专家 v8.6

    万峰图片批量处理专家,界面简洁易用,功能强大实用.支持多种处理任务同时按顺序执行,真正的批量图片,批量效果处理.支持图片批量自定义的放大缩小,旋转或者翻转,支持图片格式批量转换.支持图片批量文字水印, ...

  9. 《大型网站系统与Java中间件实践》读书笔记

    分布式系统的基础知识 阿姆达尔定律 多线程交互模式 互不通信,没有交集,各自执行各自的任务和逻辑 基于共享容器(如队列)协同的多线程模式->生产者-消费者->队列 通过事件协同的多线程模式 ...

  10. Redis的AOF是怎么实现的

    今天通过阅读AOF的实现代码,牵出了许多本来不是必须的话题,也都记下来先: Redis自己搞了一套事件循环机制: http://itindex.net/detail/26944-redis-%E4%B ...