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. 几种filter的比较

    需要整理 Gaussian filter https://en.wikipedia.org/wiki/Gaussian_filter Laplacian/Laplacian of Gaussian h ...

  2. How to programmatically new a java class which implements sepecified interface in eclipse plugin development

    http://w3facility.org/question/how-to-programmatically-new-a-java-class-which-implements-sepecified- ...

  3. YChaos生成混沌图像

    YChaos是一款通过数学公式生成混沌图像的软件,展示混沌之美,数学之美.软件中定义一套简易的脚本语言,用于描述数学表达式.使用时需要先要将数学表达式写成该脚本的形式,解析脚本代码以生成相应的图形与图 ...

  4. 查看mysql版本的四种方法

    1:在终端下:mysql -V. 以下是代码片段: [shengting@login ~]$ mysql -V mysql Ver 14.7 Distrib 4.1.10a, for redhat-l ...

  5. mac下忘记mysql密码, 重新设置mysql密码

    step1: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server)step2:进入终端输入:cd /usr/local/m ...

  6. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

  7. 首次构建android studio gradle 下载缓慢的问题

    1.先使用其他工具下载gradle, https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 2.然后放在C:\Users\A ...

  8. 给The Lab Renderer for Unity中地形添加阴影

    The Lab Renderer for Unity是Valve针对VR在Unity的体验渲染器,提高VR的渲染效率,更多的大家可以查相应资料,在这,说个The Lab Renderer for Un ...

  9. C++14 make code cleaner

    在C++11中我们如果要写一个通过tuple实现函数调用的函数要这样写: template<int...> struct IndexTuple{}; template<int N, ...

  10. Winform中进行MD5加密

    Winform,c#进行MD5加密直接上步骤: 1.添加引用 2.在.NET选项卡中找到“System.Web” 3.选中之后,点击“确定”即可,此时会在解决方案中看到刚才添加的引用 4.引用名空间: ...