leetcode第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B 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)).
首先要知道什么是median,也就是我们所知的中位数,处于中间位置的数,很简单,如果长度为奇数,那么中位数就是中间的数,如,1,2,4,那么中位数就是2。如果长度为偶数,那就是中间两个数的均值,如,1,2,3,4,那么中位数就是2.5。
Suool在http://blog.csdn.net/suool/article/details/38343457中提到的第一种方法如下:
合并,排序,找中间位置的值就好:
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int *a=new int[m+n];
memcpy(a,A,sizeof(int)*m);
memcpy(a+m,B,sizeof(int)*n);
sort(a,a+n+m);
double median=(double) ((n+m)%? a[(n+m)>>]:(a[(n+m-)>>]+a[(n+m)>>])/2.0);
delete a;
return median;
}
};
但是我们可以看到中间有个排序sort(a,a+n+m)这个复杂度已经是超过要求了。但是奇怪的是在leetcode上测试居然通过了。
还有一种方法在上述链接中也提到了,就是先在两个数组中找第k个值,然后根据奇数偶数确定k就好。但是该链接上给出的代码是错误了,通过不了。
我找到了原因,把正确的贴出如下:
class Solution
{
private:
double findKth(int a[], int m, int b[], int n, int k)
{ //find kth small
int i = ;
int j = ;
int index = ;
int kth;
if (m == )
return b[k-];
if (n == )
return a[k-];
if (k ==)
return a[]>b[] ? b[] : a[];
while(index <= k && i < m && j < n)
{
if( a[i] >= b[j])
{
index ++;
kth = b[j];
j ++;
}
else
{
index ++;
kth = a[i];
i ++;
}
}
if( index <= k && j == n) // must be <= can't be only <
{
kth = a[i+k-index];
}
if (index <= k && i == m)
kth = b[j+k-index];
return kth;
}
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m + n;
if (total & 0x1) // odd
return findKth(A, m, B, n, (total + ) / );
else // even
return (findKth(A, m, B, n, total / )
+ findKth(A, m, B, n, total / + )) / ;
}
};
原链接的代码在线测试时会出现本来是100000.5的却错误输出为100000.25,原因是原作者在k==2的时候假设错误了,并且在之后的index中没有考虑到index到了k值但是还没有达到第k小,要用小于等于号,而不是小于号,详见上述代码中注释。修改后就通过了。
暂且知道这两种方法,暂不深究了,继续前行
leetcode第二题--Median of Two Sorted Arrays的更多相关文章
- 乘风破浪:LeetCode真题_004_Median of Two Sorted Arrays
乘风破浪:LeetCode真题_004_Median of Two Sorted Arrays 一.前言 说到算法,最难的就是一些需要通过分析得到一些递推公式或者有用的结论,进而用来解决问题的方法了. ...
- LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【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 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...
- Leetcode Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 【一天一道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解题报告—— 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 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
随机推荐
- 世界上速度最快的输入法 Fleksy 为了支持中国
在 Windows Phone 8.1 内置 Word Flow 宣布取得世界最快输入记录后仅几天就打破的 Fleksy 開始測试中文支持了. 总算有一次创新的输入模式里,中文不是被落下的那个.Fle ...
- 【Eclipse高速发展-文章插件】google V8,对于调试Nodejs
安装的的时候能够通过 Help>Eclipse Marketplace的 搜索方式安装 或者 Help>Install New Software Name:googleV8 Locatio ...
- SQL入门学习3-数据更新
4-1 数据的插入(INSERT语句的使用方法) 使用INSERT语句可以向表中插入数据(行).原则上,INSERT语句背刺执行一行数据插入. CREATE TABLE 和INSERT 语句,都可以设 ...
- [置顶] think in java interview-高级开发人员面试宝典(二)
从现在开始,以样题的方式一一列出各种面试题以及点评,考虑到我在前文中说的,对于一些大型的外资型公司,你将会面临全程英语面试,因此我在文章中也会出现许多全英语样题. 这些题目来自于各个真实的公司,公司名 ...
- hdu 5072 Coprime(同色三角形+容斥)
pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...
- 【cocos2d-js公文】十八、Cocos2d-JS v3.0物业风格API
1. 新的API风格 我们直接来看看你能够怎样使用Cocos2d-JS v3.0: 曾经的API 新的API node.setPosition(x, y); node.x = x; node.y = ...
- hexo 部署至Git遇到的坑
查找资料的时候发现了next这个博客主题,next!非常的漂亮,顺手查看了hexo的相关部署. Hexo官方介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲 ...
- linux内核包转发过程(三)NIC帧接收分析
[版权声明:转载请保留源:blog.csdn.net/gentleliu.邮箱:shallnew*163.com] 每一个cpu都有队列来处理接收到的帧.都有其数据结构来处理入口和出口流量,因此.不同 ...
- python2.7和 python3.4但是不要
很多 Python 刚開始学习的人想知道他们应该从 Python 的哪个版本号開始学习.对于这个问题我的答案是 "你学习你喜欢的教程的版本号.然后检查他们之间的不同. " 可是假设 ...
- 使用 CodeIgniter 框架快速开发 PHP 应用(四)
原文:使用 CodeIgniter 框架快速开发 PHP 应用(四) 使用 CI 简化数据库开发你学习CI 是因为你想要使编程更容易和更有生产力.这一章讲述CI的Active Record类. 如果C ...