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 ...
随机推荐
- OpenWRT GPIO人口控制 WLED
Linux根据系统GPIO系统架构简介 关于这个GPIO我一直认为非常非常长的时间easy.但.当你需要给一个特定的系统,参与这些GPIO什么时候.你会找到.不对,实例,mt7620n. GPIO#7 ...
- B/S在北大青鸟-ASP.NET 总结
一个.前言: 这几周跟着于海涛老师进入了.NET编程世界.领略到了ASP.NET的精髓. 要说起ASP.NET的发展史,那要追溯到HTML了,由于它功能简单,无法从用户接收信息并自己主动进行更新.而不 ...
- Android高级编程笔记(四)深入探讨Activity(转)
在应用程序中至少包含一个用来处理应用程序的主UI功能的主界面屏幕.这个主界面一般由多个Fragment组成,并由一组次要Activity支持.要在屏幕之间切换,就必须要启动一个新的Activity.一 ...
- PKU 1509 Glass Beads (最小表示法)
题意:有一个环形字符串,让你找一个位置切一刀使得字符串字母序最小.输出这个位置. 思路:能够看成两个字符串比較.一个是从下标0開始(0~n-1),一个从下标1開始(1~n-1,0). 然后两个指针i= ...
- 二维码(QR Code)生成与解析
二维码(QR Code)生成与解析 写在前面 经常在大街上听到扫码送什么什么,如果真闲着没事,从头扫到位,估计书包都装满了各种东西.各种扫各种送,太泛滥了.项目中从没接触过二维码的东东,最近要使用,就 ...
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme
配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_lau ...
- Redis源代码分析(十)--- testhelp.h小测试框架和redis-check-aof.c
日志检测
周期分析struct结构体redis代码.最后,越多越发现很多的代码其实大同小异.于struct有袋1,2不分析文件,关于set集合的一些东西,就放在下次分析好了,在选择下个分析的对象时,我考虑了一下 ...
- 中国人被“清朝GDP世界第一”忽悠了!
中国人被"清朝GDP世界第一"忽悠了!"鸦片战争前的清朝GDP世界第一",这一说法在中国流传非常广.追根溯源,最早提出这一观点的似乎是英国学者麦迪森,他的一项猜 ...
- android 之 下载管理器 无论监测在当地的设计思路
我相信你.让应用市场.或其它下载,想不管是什么地方监测进展情况.而且很好的实现. 这里分享一个相对简单的,并防止内存溢出等..我们用一个引用弱结合View进展更新方法. Map<String, ...
- Maven学习笔记(四):协调和依赖
Maven协调具体的解释: Maven定义了这样一组规则:世界上不论什么一个构件都能够使用Maven坐标唯一标识.Maven坐标的元素包含groupId.artifactId.version ...