【题目描述】

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

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5

【解题思路】

1、整体排序,去中间值,但是,实际上,我们不需要完整的排序完成,只需要排序middle左边的即可,这样就能保证middle所在的位置是中间位置。

2、需要考虑num1和mun2全部长度为偶数和奇数的问题,因为如果是奇数,中间值为一个,如果是偶数,中间值为两个。这个其实,终归是要在两个数组中查找一个具体的值。

3、当一个数组已经遍历完了,但没有找到值时,要在另外一个数组中继续遍历。

【代码实现】

public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
int total = n1 + n2;
if(total % 2 == 1)//奇数情况
{
return findMiddle(nums1, nums2, n1, n2, (total/2 + 1));
}
else
{
return (findMiddle(nums1, nums2, n1, n2, (total/2)) + findMiddle(nums1, nums2, n1, n2, (total/2 + 1)))/2;
} } private double findMiddle(int[] nums1, int[] nums2, int n1, int n2, int midIndex)
{
double middle = 0.0;
int i = 0, j = 0;
for(; i<n1 && j<n2; )
{
if(nums1[i] < nums2[j])
{
i++;
middle = nums1[i-1];
}
else
{
j++;
middle = nums2[j-1];
}
if((i+j) == midIndex)
{
break; }
}
while(i<n1 && ((i+j) < midIndex))
{
i++;
middle = nums1[i-1];
if((i+j) == midIndex)
{
break;
}
}
while(j<n2 && ((i+j) < midIndex))
{
j++;
middle = nums2[j-1];
if((i+j) == midIndex)
{
break;
}
}
return middle;
}

【后续】

发现了更好的方法,通过分治来实现,方法确实巧妙。

可参考链接:

https://hk029.gitbooks.io/leetbook/%E5%88%86%E6%B2%BB/004.%20Median%20of%20Two%20Sorted%20Arrays[H]/004.%20Median%20of%20Two%20Sorted%20Arrays[H].html

leetCode之Median of Two Sorted Arrays的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  3. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  5. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  8. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  9. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  10. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

随机推荐

  1. 播放48KHZ有1S的停顿

    两个音频文件: /usr/lib/gstreamer-0.10/libgstflump3dec.so /usr/lib/gstreamer-0.10/libgstflumpegdemux.so

  2. sublime text3 授权码

    适用于 Sublime Text 3 Build3126 64位 官方版 -– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821 ...

  3. Mybatis <if>标签使用注意事项

    在<if>标签的test中,不能写成“name !='aa'” , 会报错### Error querying database. Cause: java.lang.NumberForma ...

  4. 位运算 进制转化 STL中bitset用法

    2017-08-17 16:27:29 writer:pprp /* 题目名称:输入十进制以二进制显示 程序说明:同上 作者:pprp 备注:无 日期:2017/8/17 */ #include &l ...

  5. 关于ckeditor 之 上传功能

    度了很多文章,看了很多关于ckeditor配置上传功能的文章,没一个写得清楚的, 就是简单的根目录下.config.js 增加 config.filebrowserUploadUrl="/a ...

  6. 简单的使用hibernate插入数据的例子

    数据库创建脚本: drop table person create table person( id          varchar(32)         not null primary key ...

  7. ColKang v1.0

    /* *2015.3.31 14:00更新 *上午刚写完这篇博客,下午就读到迭代器了.C++ primer中讲迭代器那节说道了->符号的意思,即(*ptr).  及将指针解引用之后再调用成员函数 ...

  8. android AVD启动失败原因之一

    在mac上安装好Android SDK.AVD及相关的组件之后,手动创建了一个安卓模拟器后,通过actions启动,会弹出一个提示窗口,然后就闪退,也没有报错什么的,在网上搜了半天AVD启动失败的问题 ...

  9. 蓄水池抽样算法 Reservoir Sampling

    2018-03-05 14:06:40 问题描述:给出一个数据流,这个数据流的长度很大或者未知.并且对该数据流中数据只能访问一次.请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等. 问题求 ...

  10. Android真机调试——远程主机强迫关闭了一个现有的连接。

    以前用真机调试程序的时候,Android Studio 出现如下的错误 [2016-11-12 10:37:36 - DeviceMonitor] Adb connection Error:远程主机强 ...