题目:

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

Subscribe to see which companies asked this question

解题思路:

  我自己想的方法,先排序在查找。两个数组,首先想到是归并排序,然后再查找两个数组合并之后的中间元素即为中位数。我们分析下时间复杂度主要用在了归并排序上,为O((m+n)log(m+n)),显然不符合题目要求。题目要求是O(log(m+n)),但是我将这个方法的代码提交上去,仍然通过了,说明LeetCode的编译平台并没有严格按照ACM OJ这种要求来设置。排序后查找的代码如下所示:

 //方法一:归并排序后查找:O((m+n)lg(m+n)),奇怪竟然通过了
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
size_t n1 = nums1.size(), n2 = nums2.size();
size_t n = n1+n2;
vector<int> nums(n,); assert(n > ); nums1.push_back(INT_MAX);
nums2.push_back(INT_MAX); size_t i = , j = , k = ;
while(i < n1 || j < n2) {
if (nums1[i] <= nums2[j]) {
nums[k++] = nums1[i++];
}
else
nums[k++] = nums2[j++];
} return ((n%) ? (double)nums[(n-)/]:(double)(nums[(n-)/]+nums[n/])/);
}

  

  看了下本题的难度系数,属于Hard级别的,说明本题不是那么容易对付的,又看了一下本题的Tag,其中罗列了两个重要的Tag:Divide and Conquer和Binary Search,说明本题需要用到两个方法:分治法和二分查找法,看了讨论里面,发现一种方法是这样的:求有序数组A和B有序合并之后第k小的数!如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明。详细的思路请见这篇博文。代码如下:

 //方法二:二分法:O(lg(m+n)),满足题目要求
//get the kth number of two sorted array
double findkth(vector<int>::iterator a,int m,
vector<int>::iterator b,int n,
int k)
{
if(m > n)
return findkth(b,n,a,m,k);
if(m == )
return b[k-];
if(k == )
return min(*a,*b); int pa = min(k/,m),pb = k - pa;
if(*(a + pa - ) < *(b + pb -))
return findkth(a+pa,m-pa,b,n,k-pa);
else if(*(a + pa -) > *(b + pb -))
return findkth(a,m,b+pb,n-pb,k-pb);
else
return *(a+pa-);
} double findMedianSortedArrays1(vector<int>& nums1, vector<int>& nums2) {
vector<int>::iterator a = nums1.begin();
vector<int>::iterator b = nums2.begin();
int total = nums1.size() + nums2.size(); // judge the total num of two arrays is odd or even
if(total & 0x1)
return findkth(a,nums1.size(),b,nums2.size(),total/+);
else
return (findkth(a,nums1.size(),b,nums2.size(),total/) + findkth(a,nums1.size(),b,nums2.size(),total/ + ))/;
}

LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard的更多相关文章

  1. 60.Median of Two Sorted Arrays(两个排序数组的中位数)

    Level:   Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...

  2. 4. Median of Two Sorted Arrays[H]两个有序数组的中位数

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the midian of the ...

  3. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  4. Leetcode4--->求两个排序数组的中位数

    题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...

  5. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  6. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

  7. LeetCode-4. 两个排序数组的中位数(详解)

    链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...

  8. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

  9. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. 每周一道数据结构(四)A*算法&博弈树α-β剪枝

    A*算法/博弈树 前阵子考试学了A*算法.博弈树和回溯,自己真是愚蠢至极,根本没就搞明白这些,所以对于这些算法问道的话就不能说清楚,也记不住,所以才有了这篇笔记.在这里感谢面试我的那位工程师~~ A* ...

  2. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  3. C语言实现单链表-03版

    在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...

  4. paip.slap工具与于64位win7与JDBC的性能对比

    paip.slap工具与于64位win7与JDBC的性能对比 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog ...

  5. atitit...触发器机制 ltrigger mechanism sumup .的总结O8f

    atitit...触发器机制  ltrigger  mechanism sumup .的总结O8f 1. 触发器的类型 1 2. 实现原理 1 3. After触发器 Vs Instead Of触发器 ...

  6. android 给空白包签名

    前言:之前公司app在腾讯开放平台认领应用时,涉及了一个问题:就是给空白包签名.然后再上传上去审核. 腾讯开放平台的官方说明如下, 如何签名:jarsgner-verbose-keystore[key ...

  7. Correct use of System.Web.HttpResponse.Redirect

    from:https://blogs.msdn.microsoft.com/tmarq/2009/06/25/correct-use-of-system-web-httpresponse-redire ...

  8. GTD时间管理(2)---管理收集箱

    通过上面一篇文章,相信大家对GTD收集有了原理大致的了解,如果大家对收集不是很了解,可以去看一下. 当我们收集到很多想法和事情之后,在晚会的时候必须要清空收集箱,否则收集箱会堆积如山,最终收集箱成了垃 ...

  9. ZooKeeper典型应用场景一览

    原文地址:http://jm-blog.aliapp.com/?p=1232 ZooKeeper典型应用场景一览 数据发布与订阅(配置中心) 发布与订阅模型,即所谓的配置中心,顾名思义就是发布者将数据 ...

  10. http 请求类

    1.httpclient请求类 代理demo:http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/examples/org/apac ...