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 ...
随机推荐
- iOS相册、相机、通讯录权限获取
iOS相册.相机.通讯录权限获取 说明 这是本人写的一个工具,用以便利的处理各种权限获取的操作,目前提供相册.照相机.通讯录的权限获取操作,参考了 http://www.jianshu.com/p/a ...
- [C] tcharall(让所有平台支持TCHAR)v1.1。源码托管到github、添加CMake编译配置文件、使用doxygen规范注释
作者:zyl910 v1.1版的改动如下—— 将源码上传到github. 调整目录结构. 添加CMake编译配置文件. 使用doxygen规范注释. 文件清单—— docs\ docs\images\ ...
- 机器学习基石--学习笔记01--linear hard SVM
背景 支持向量机(SVM)背后的数学知识比较复杂,之前尝试过在网上搜索一些资料自学,但是效果不佳.所以,在我的数据挖掘工具箱中,一直不会使用SVM这个利器.最近,台大林轩田老师在Coursera上的机 ...
- android 中 ViewPager 的平常用法 ViewPager+ Views
延续前面几个的经常用到的ViewPager, 直接加载各种不同的 View 工程目录: 代码: public class ViewActivity extends Activity { // 每个Vi ...
- Java Arrays 排序
Java SDK中的排序分为两种情况: .对基础类型数组的排序,使用DualPivotQuicksort类 a.如果是对char.short数组的排序,因为byte.char.short分别为8bit ...
- Pro ASP.NET MVC –第六章 MVC的基本工具
在本章,我们将介绍每个MVC程序员"武器库"的三个重要工具:依赖注入容器.单元测试框架和mock工具.在本书,对于三个工具分别都只用了一种方式实现,但每个工具都还有其他的实现方式. ...
- oracle小数点前零丢失的问题
1.问题起源 oracle 数据库 字段值为小于1的小数时,使用char类型处理,会丢失小数点前面的0 例如0.2就变成了.2 2.解决办法: (1)用to_char函数 ...
- JVM 参数翻译汉化解释
博客搬家,新地址:http://www.zicheng.net/article/38.htm Behavioral Options(行为参数) Option and Default Value Des ...
- HTTP协议学习
面试过程中又一个常见的问题,http协议,因为做服务器开发如果用http协议的话,现在各种开源软件都封装好了,python中只需要简单的继承定义好的类,重写get或者post等方法,几行代码就可以搭建 ...
- HTTP 头部详细解释
HTTP 头部解释 ================================================ Accept 告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,ty ...