题目:

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. 深入理解java虚拟机【Java Class类文件结构】

    Java语言从诞生之时就宣称一次编写,到处运行的跨平台特性,其实现原理是源码文件并没有直接编译成机器指令,而是编译成Java虚拟机可以识别和运行的字节码文件(Class类文件,*.class),字节码 ...

  2. B-tree&B+tree

    B-tree&B+tree B-tree,B是balance,一般用于数据库的索引.使用B-tree结构可以显著减少定位记录时所经历的中间过程,从而加快存取速度.而B+tree是B-tree的 ...

  3. [C++] socket - 5 [API事件对象实现线程同步]

    /*API事件对象实现线程同步*/ #include<windows.h> #include<stdio.h> DWORD WINAPI myfun1(LPVOID lpPar ...

  4. [游戏模版1] MFC最小框架(base function including)

    >_<:Here are the template of mini-MFC include: CPen,CBrush,Front,Paint Line and some other gra ...

  5. MySQL的pt-query-digest的下载与使用

    对于脚本文件,是可以执行的,我们不用安装.所以,但是这个脚本文件没有执行的权限,所以,我们首先赋予这个脚本文件的可执行的权限. 再次查看文件的信息后. 已经有了执行的权限了. 运行脚本的时候,可要注意 ...

  6. HandlerMapping 详解

    HandlerMapping 详解 1. 导言 万丈高楼平地起,SpringMVC的辉煌离不开每个组件的相互协作,上一章详细阐述了SpringMVC整个体系结构及实现原理,知道HandlerMappi ...

  7. 在 C++Builder 工程里调用 DLL 函数

    调用 Visual C++ DLL 给 C++Builder 程序员提出了一些独特的挑战.在我们试图解决 Visual C++ 生成的 DLL 之前,回顾一下如何调用一个 C++Builder 创建的 ...

  8. Activiti 删除流程定义

    package com.mycom.processDefinition; import java.io.InputStream; import java.util.List; import java. ...

  9. VS2010链接TFS

    VS2010链接TFS源代码管理器 1.打开VS2010开发工具. 2.菜单视图===>>团队资源管理器 3.点击链接到团队项目 4.点击服务器 5.点击添加 6.输入TFS服务配置信息 ...

  10. 转:最简单的基于 DirectShow 的视频播放器

    50行代码实现的一个最简单的基于 DirectShow 的视频播放器 本文介绍一个最简单的基于 DirectShow 的视频播放器.该播放器对于初学者来说是十分有用的,它包含了使用 DirectSho ...