leetcode-【hard】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 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的更多相关文章
- 【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 ...
- 【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 ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- 【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 ...
- 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 ...
- (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 ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【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 ...
- 【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 ...
随机推荐
- 在shell script中进行数值运算的两种方法
方法1:使用"$((计算式))"的方式进行数值运算,不需要使用declare命令显示声明数值型变量来存储计算结果: 方法2:使用declare命令配合"-i"选 ...
- 入门级 mongodb
谢谢 转载的.. (1)下载mongodb window版本:http://www.mongodb.org/dr/downloads.mongodb.org/win32/mongodb-win32 ...
- online_jf.lua --累计在线时间领取物品(积分)的lua脚本
原作者: ayase 8-27修正 修复首次使用后的红字不需要额外进数据库导入计分表,这lua全自动生成 ----------------------------------------------- ...
- LoadRunner 11 安装及破解
LoadRunner 11 安装及破解 前提条件: 内存:2G,硬盘空闲空间10G,安装完成后实际只占不到2G 支持winXP SP3;32位与64位win7浏览器支持IE6-8,IE9,fir ...
- Pads怎么设置某一网络的线宽
在利用pads工具进行layout时,由于某一条网络可能会有很多条走线,而走线的宽度也相同,如果一条条设置,会很麻烦,所以pads中可以直接设置某一网络的线宽,避免繁琐的工作. 如下所示同一网络的走线 ...
- Qt开发中的实用笔记三--关于各种类的零碎知识点:
1,QUuid()创建唯一标识码,在创建数据库实体ID和链接数据库QSqlDatabase时非常方便 2,QScrollArea与QScrollBar,如果是要在widget中添加窗口滑动QScrol ...
- js调用页面打印
----------------------调用页面打印-------------------------------- <body> <div id="divPrint& ...
- SQL Server常用的性能诊断语句
/* 常规服务器动态管理对象包括: dm_db_*:数据库和数据库对象 dm_exec_*:执行用户代码和关联的连接 dm_os_*:内存.锁定和时间安排 dm_tran_*:事务和隔离 dm_io_ ...
- 压力测试报出503错误---ASP.NET支持大并发的相关配置
项目反馈报出503错误,需要收集性能数据如下: 1.Windows性能监视器,该应用程序池进程的线程和处理队列 2.问题重现时的进程dump 这是请求到达IIS后遇到的第一个队列,HTTP.sys收到 ...
- there are no usable controls in this group
今天遇到一个怪事, MFC的toolbox是灰的, 不能使用, 后来上网一查找到解决方案: 右键Toolbox, 点击"Choose Items", 重新启动VS2013, 这样t ...