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 ...
随机推荐
- eclipse 引用自己开发的模块
这样就可以 生成的是LIB 工程需要设置“Is Library”
- 从一个例子中体会React的基本面
[起初的准备工作] npm init npm install --save react react-dom npm install --save-dev html-webpack-plugin web ...
- 【转】升级Xcode6.3插件失效解决办法
1.打开终端,输入以下代码获取到DVTPlugInCompatibilityUUID defaults read /Applications/Xcode.app/Contents/I ...
- [Aaronyang] 写给自己的WPF4.5 笔记19[Visual类图文并茂讲解]
文章虽小,内容还好,且看且珍惜. aaronyang版权所有,不许转载,违者必究 当界面上使用数千个矢量图形,例如实时统计图,粒子碰撞,比如超级玛丽游戏,图像一直在绘,过量的使用WPF的元素系统和Sh ...
- Scala 深入浅出实战经典 第40讲:Set、Map、TreeSet、TreeMap操作代码实战
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- python字符串格式化方法 format函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序 ...
- CentOS 6.5 EasyPR环境搭建
EasyPR是一款开源的中文车牌识别系统,项目地址. 在搭建的过程中,主要的问题是注意版本的兼容性,这里面的版本包括:opencv版本,g++版本以及cmake版本. 我使用的EasyPr版本信息如下 ...
- 分享一个点赞超过100的漂亮ASP.NET MVC蓝色界面框架
从 陈贞宝 博客中看到一个MVC模板感觉特别漂亮就尝试着分离出来,直接拿来用啦,直接拷贝到自己的常用的代码库里收藏起来,地址是http://www.cnblogs.com/baihmpgy/p/381 ...
- 通过js看类似C#中的回掉
我认为并行有两种形式,第一种是异步,第二种是多线程,目的都是为了实现并行,只不过异步和多线程都是手段而已 第一种异步 异步,执行完函数或方法后,不必阻塞性地等待返回值或消息,只需要向系统委托一个异步过 ...
- JavaScript跨域方法
一.什么是跨域 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: ...