题目

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. Python 批量修改文件名

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

  3. HDU 5936 Difference

    题意: 有一个函数f(y, k) = y的每个十进制位上的数字的k次幂之和 给x, k 求 有多少个y满足 x = f(y, k) - y 思路: (据说这叫中途相遇法?) 由于 x >= 0 ...

  4. 关于android R.layout.没有出现自己写的布局解决方法

    直接上解决方法 1.若import了androi.R直接删除,导入自己包下的R文件,例如com.example.my.R 2.若上面的做了后还有错误,应该是包名不一致导致的,即com.example. ...

  5. 空的安卓工程添加activity

    1.编写类继承activity,并重写onCreate方法 package org.tonny; import android.app.Activity; import android.os.Bund ...

  6. C#连接操作mysql实例

    第三方组件:Mysql.Data.dll说明:去官方网站下载Mysql.Data.dll,然后在项目中添加该组件的引用,在代码页里输入using Mysql.Data.MysqlClient,我们就可 ...

  7. 发布 Ionic iOS 企业级应用

    转自:http://www.jianshu.com/p/c12fae498975 Ionic 项目开发完成之后呢,自然就是打包发布了,今天说说 iOS 的打包和发布.iOS 的发布方式分为三种:iOS ...

  8. python3-day2-python基础2

    一.for循环 for循环是我们编程中非常常用的一种循环,以下就是for循环在python中的一些应用实例: 1.单层for循环 #!/usr/bin/env python3#-*- coding: ...

  9. chmod、chown、chgrp的意思

    文件权限管理chown->change owner 改变文件所有者chown test install.log -将install.log这个文件的所有者改为test用户 chgrp->c ...

  10. Inno Setup制作安装包的几个问题

    1. 卸载时,如何判断应用程序是否运行    InnoSetup 提供变量AppMutex,用来保存应用程序的Mutex名称.现在很多应用程序都是唯一实例运行.这样避免配置文件被错误修改以及其他很多衍 ...