LeetCode—— Median of Two Sorted Arrays
Description:
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)).
思路:找出两个已经排好序数组的中位数。可以使用合并排序中的merge,然后直接找出中位数就能AC。时间复杂度为O(m+n)。但是!!这毕竟是一个Hard的题!时间复杂度要求O(log(m+n))!
先上merge代码:
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = 0, n = 0;
if(nums1 != null) {
m = nums1.length;
}
if(nums2 != null) {
n = nums2.length;
}
int[] res = new int[m + n];
//merge
int cur = 0, i = 0, j = 0;
while(i<m && j<n) {
if(nums1[i] < nums2[j]) {
res[cur] = nums1[i];
i ++;
}
else {
res[cur] = nums2[j];
j ++;
}
cur ++;
}
while(i < m) {
res[cur] = nums1[i];
i ++;
}
cur ++;
while(j < n) {
res[cur] = nums2[j];
j ++;
cur ++;
}
//findMedian
int length = m + n;
double ans = 0.0;
//
if((length & 1) != 0) {
ans = res[length / 2];
}
else {
ans = (double)(res[length/2 - 1] + res[length/2]) / 2.0;
}
return ans;
}
}
严重怀疑Java的测试数据和C/C++的不一样。要不效率会这么高!

O(log(m+n))的二分代码网上都是一样的,就不贴了。
http://blog.csdn.net/zxzxy1988/article/details/8587244
LeetCode—— Median of Two Sorted Arrays的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
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 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- LeetCode——Median of Two Sorted Arrays
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
随机推荐
- Skyline6.5系列覆盖三维地理信息产业上下游
SkylineGlobe将于近日推出6.5 系列产品.该系列产品提供从产业链上游影像处理.中游二三维展示分析.下游具体业务应用等覆盖整个三维空间地理信息产业链的一体化.一站式产品与服务. Skylin ...
- Scala 深入浅出实战经典 第68讲:Scala并发编程原生线程Actor、Cass Class下的消息传递和偏函数实战解析
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...
- IOS中的多线程之GCD
在ios中,使用多线程有三种方式,分别是:NSThread.NSOperation和NSOperationQueue.GCD,在本节,主要讲解一下CDD的使用. GCD(Grand Central D ...
- Ques前端组件化体系
Ques是一套组件化系统,解决如何定义.嵌套.扩展.使用组件. 传统开发模式的痛点 无法方便的引用一个组件,需要分别引用其Javascript.Template.CSS文件 我们期望能以MV*的方式去 ...
- OpenStack云计算(二)——OpenStack 计算
http://www.ibm.com/developerworks/cn/cloud/library/cl-openstack-nova-glance/
- sqlite 的比较等运算是根据不同的值而不同的,并不是根据的字段类型,因为 sqlite 是弱类型字段
sqlite 的比较等运算是根据不同的值而不同的,并不是根据的字段类型,因为 sqlite 是弱类型字段 --------------------------------------------- ...
- 【Linux】——ctags
ctags的功能:扫描指定的源文件,找出其中所包含的语法元素,并将找到的相关内容记录下来. ctags 可以在官网上下载源代码,然后编译安装.最后在 ~/.vimrc 文件中写入以下配置: " ...
- HDU 4107 Gangster Segment Tree线段树
这道题也有点新意,就是须要记录最小值段和最大值段,然后成段更新这个段,而不用没点去更新,达到提快速度的目的. 本题过的人非常少,由于大部分都超时了,我严格依照线段树的方法去写.一開始竟然也超时. 然后 ...
- 浅谈html5网页内嵌视频
更好的阅读体验:浅谈html5网页内嵌视频 如今在这个特殊的时代下:flash将死未死,微软和IE的历史问题,html5标准未定,苹果和谷歌的闭源和开源之争,移动互联网的大势所趋,浏览器各自为战... ...
- 多条件动态LINQ 组合查询
本文章转载:http://www.cnblogs.com/wangiqngpei557/archive/2013/02/05/2893096.html 参考:http://dotnet.9sssd.c ...