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 ...
随机推荐
- STM32学习笔记——点亮LED
STM32学习笔记——点亮LED 本人学习STM32是直接通过操作stm32的寄存器,使用的开发板是野火ISO-V2版本: 先简单的介绍一下stm32的GPIO: stm32的GPIO有多种模式: 1 ...
- 给linux安全模块LSM添加可链式调用模块(一)
前些日子接了个外包的活,了解了一下Linux安全模块,发现了安全模块中的一些问题. 关于linux安全模块LSM在此就不多说了,大家google下就明白了. 这里主要介绍的是如何修改这个模块,使它可链 ...
- Java中避免表单重复提交
表单的重复提交: 没有完整的进行一次,先请求表单页面->再提交表单过程而完成数据提交 造成的根本原因: 没有完整的进行一次,先请求表单页面->再提交表单过程. 造成重复提交的现象: 由于服 ...
- vue 2.0版本----》常用代码说明
------------------------------------------代码不显示-------------------------------------------- [v-cloak ...
- Python>>>Flask框架使用之入门
操作平台Windows Python2.7 安装 pip install flask Hello World程序 from flask import Flask app = Flask(__name_ ...
- nginx入门篇----nginx服务器基础配置
1.nginx.conf文件结构... #全局块 events{ ... } http #http块{ ...
- Linux java环境安装
一.jdk yum 安装方法 1.wegt http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260 ...
- asp.net 导入excel文件
前台页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b2ccarri ...
- WebLogic Server官方下载地址
WebLogic Server官方下载地址 英文(更新到12c和9.2.4):http://www.oracle.com/technetwork/middleware/ias/downloads/wl ...
- knockout+bootstrap--一些复杂的应用合集
一.针对My97日历控件的绑定 普通绑定和特殊格式绑定(红色部分) <!-- ko foreach: items --> <td class="ruyeeTableTDLa ...