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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

  4. 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 ...

  5. 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 ...

  6. C++ Leetcode Median of Two Sorted Arrays

    坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...

  7. 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 ...

  8. 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的平均值.故偶数数目的中间 ...

  9. LeetCode Median of Two Sorted Arrays 找中位数(技巧)

    题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...

随机推荐

  1. spring中使用mockito

    1 mockito介绍和入门 官方:https://github.com/mockito/mockito 入门: 5分钟了解Mockito http://liuzhijun.iteye.com/blo ...

  2. Andriod Studio 开发环境安装和配置

    Android Studio安装配置详细步骤(图文):http://www.2cto.com/kf/201604/500642.html第一次使用Android Studio时你应该知道的一切配置 : ...

  3. ATT 解锁手机

    最近豪们都在忙着买买买950 本土鳖左瞅瞅右看看实在钱包不够豪 正好看到美帝640打折39刀 (http://www.microsoftstore.com/store/msusa/en_US/pdp/ ...

  4. android 左右翻页

    布局: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android ...

  5. ECSHOP始终显示全部分类方法

    商品分类树需要始终显示所有类别,默认的Ecshop的显示方式为在当前产品页面只显示当前的产品所在的同级及下级分类,这就导致当点开某个产品或者子分 类的时候全局的分类树就不见了. 其实修改的方法很简单. ...

  6. SIP:用Riverbank的SIP创建C++库的Python模块

    我们发现PyQt做的Python版的PyQt是如此好用,如果想把自己的C++库包装成Python模块该如何实现呢? 这里介绍下用SIP包装C++库时值得参考的功能实现: 需要Python模块中实现C+ ...

  7. Android上面安装Linux的方法

    方法一: 并行安装Linux(不在Android操作系统之上运行,需要设备已经unlocked并且rooted) 我还没玩过.放两个书签: How to Install Ubuntu on Andro ...

  8. ikvm.net简介

    ikvm.net是什么 http://www.ikvm.net/ ikvm.net是能够运行在mono和.net framework的java虚拟机.它包括了 在.net中实现的一个java虚拟机 j ...

  9. 查看、关闭linux自启动网络服务

    1.查看 netstat --tulnp ..master  smtp 服务 2.关闭 /etc/init.d/服务 stop 停止 : start 启动 chkconfig 服务 off  关闭   ...

  10. jqgrid单元格中增加按钮

    1.增加列配置 { label: '问题数据', name: 'action', width: 80, align: 'center' } 2.函数 gridComplete: function () ...