【题目描述】

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. MongoDB使用中的一些问题

    1.count统计结果错误 这是由于分布式集群正在迁移数据,它导致count结果值错误,需要使用aggregate pipeline来得到正确统计结果,例如: db.collection.aggreg ...

  2. 用C#连接SFTP服务器并进行上传下载文件

    1.使用软件连接可采用WinSCP进行: 文件协议选择SFTP,端口号默认22 2.使用C#代码操作 参考:http://www.cnblogs.com/binw/p/4065642.html 主要引 ...

  3. 20145312 《Java程序设计》第八周学习总结

    20145312 <Java程序设计>第八周学习总结 学习笔记 Chapter 15时间与日期 15.1 日志 15.1.1 日志API简介 java.util.logging包提供了日志 ...

  4. 2009-2010 ACM-ICPC, NEERC, Western Subregional Contest

    2009-2010 ACM-ICPC, NEERC, Western Subregional Contest 排名 A B C D E F G H I J K L X 1 0 1 1 1 0 1 X ...

  5. 重新想,重新看——CSS3变形,过渡与动画③

    这一篇主要谈谈CSS3的过渡属性. 过渡属性被设计的十分通俗易懂,属性写法为transition,有四个子属性: <transition-property> 表示需要过渡的属性[必须](本 ...

  6. ubuntu下wget的配置文件在哪里

    答:/etc/wgetrc 这个文件里可以指定代理,如: http_proxy = http://myproxy.com:8080

  7. OpenDayLight "Error executing command: java.lang.NullPointerException"问题解决

    参考: Fedora 21 mostly working but NullPointerException at Karaf shell 在使用ODL的时候,安装功能组件时出现: Error exec ...

  8. bash-文件表达式

    一点例子: #!/bin/bash # test-file: Evaluate the status of a file FILE=~/.bashrc if [ -e "$FILE" ...

  9. 你可能不知道的mouseover/mouseout mouseenter/mouseleave

    mouseover与mouseenter 1. 触发时机 mouseover在被监听的节点与子节点上都会触发 mouseenter只在被监听的节点上触发 本质上是因为mouseenter不能冒泡 2. ...

  10. 【Linux】结合Python 简易实现监控公司网站,邮件发送异常

    背景 由于一些原因,博主负责测试的网站的服务器切换到了香港,切换后出现了多次访问超时的情况 于是主动请缨写一个自动监测的脚本,本来准备完全使用shell来写,后来发现shell发送邮件只能在测试机之间 ...