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 ...
随机推荐
- 数组名取地址所算数运算应注意的"trap"
数组名取地址所算数运算应注意的"trap" 直接看代码: #include <stdio.h> int main() { int array[5]; printf(&q ...
- Java工程(3)——但从谈论用户的角度UI图案
前言: 海南项目宣告竣工,验收之日,除部分代码有待优化外,亟待改进的就是界面. 米老师说:连你都忍不住去拖下滚动栏,你还指望用户用的舒坦吗? 顿悟: 业务.功能也许是软件的核心,技术也许是软件的精髓. ...
- PHP Yii框架开发——组织架构网站重构
最近一段时间在维护公司的组织架构网站(Org),旧版网站只是用了xampp简单搭建了一套环境部署在了windows机器上,代码结构相对简单. 整个架构如下: 整个架构没有用到复杂的结构,class里放 ...
- HTTP必知必会(转)
HTTP协议作为网络传输的基本协议,有着广泛的应用.HTTP协议的完整内容很多,但是其核心知识却又简单精炼.学习者应该掌握其基本结构,并且能够举一反三.这篇文章所列的,就是在实际开发中必须知道必须掌握 ...
- JQUERY 选择
jQuery 选择器 jQuery 采用 CSS 一个选择选择 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") 选取 ...
- mysql JDBC总结
今天复习了下jdbc操作数据库,其实通过复习,感觉对类的熟悉和方法的运用都是小事,毕竟有API都可以查得到. 关键是一些设计, 1. 比如: Class.forName("");这 ...
- Android Fragment 真正彻底的解决(下一个)
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因.以及一些主要的使用方 ...
- 小强HTML5手机发展之路(52)——jquerymobile触摸互动
当使用移动设备的触摸操作,最常用的是点击.按住屏幕或手势,jQuery Mobile能够通过触摸事件绑定行为来响应用户的特定触摸. 一个.点击并按住 直接在代码(在代码中的一切,它使产品!) < ...
- javascript实现数据结构:广义表
原文:javascript实现数据结构:广义表 广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...
- firefox里面title乱码
原文:firefox里面title乱码 昨天 在notepad++里面写得文档里面title里面有中文,即使在文档里面写有charset=’UTF-8’, 但是保存后在firefox运行,浏览器标签标 ...