LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard
题目:
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的更多相关文章
- 60.Median of Two Sorted Arrays(两个排序数组的中位数)
Level: Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- Leetcode4--->求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...
- LeetCode(4):两个排序数组的中位数
Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...
- LeetCode4. 两个排序数组的中位数
4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...
- LeetCode-4. 两个排序数组的中位数(详解)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...
- JavaScript实现获取两个排序数组的中位数算法示例
本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...
- 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 ...
随机推荐
- Book Review: PowerShell 3.0 Advanced Administration Handbook
Recently I read a book, PowerShell 3.0 Advanced Administration Handbook, which I found really worthy ...
- UnicodeDecodeError while using json.dumps()
UnicodeDecodeError 系统.文件.vim全部设置为utf-8 export LANG=zh_CN.UTF8 # coding:utf-8 json.dumps(content, ens ...
- Oracle dmp文件导入(还原)到不同的表空间和不同的用户下
------------------------------------- 从生产环境拷贝一个dmp备份文件,在另外一台电脑上搭建测试环境,用imp命令导入dmp文件时提示如下错误: 问题描述: IM ...
- paip. 内存占用少的php ide选择评测总结
paip. 内存占用少的php ide选择评测总结 php ide主要以内存占用为标准进行评测.. 其次以软件体积为标准.. 作者Attilax 艾龙, EMAIL:1466519819@qq.c ...
- Atitit.现实生活中最好使用的排序方法-----ati排序法总结
Atitit.现实生活中最好使用的排序方法-----ati排序法总结 1. 现在的问题 1 2. 排序的类别::插入排序//交换排序//选择排序(每次最小/大排在相应的位置 )//归并排序//基数排 ...
- paip.代码生成器数据源格式最佳实践
paip.代码生成器数据源格式最佳实践 需要满足几个条件. 1.基于文本 2.容易的编辑.. 3,容易的代码解析. 这样,常用的设计工具powerdesign cdm pdm就排除兰...cdm虽然是 ...
- TF Boys (TensorFlow Boys ) 养成记(六)
圣诞节玩的有点嗨,差点忘记更新.祝大家昨天圣诞节快乐,再过几天元旦节快乐. 来继续学习,在/home/your_name/TensorFlow/cifar10/ 下新建文件夹cifar10_train ...
- O2O已死?不!美团点评们迎来新风口
当年的千团大战,巅峰时期曾涌入了5000多家团购网站,刘旷本人也参与了此次团购大战.而就在当时很多人都唱衰团购的时候,美团和大众点评却最终脱颖而出,市值一路飙升,人人网旗下的糯米网因为卖给了百度,也得 ...
- java防止脚本注入,通过拦截器实现
1:利用action过滤 package com.tsou.comm.servlet; import java.util.Enumeration; import java.util.Map; impo ...
- Liferay7 BPM门户开发之27: MVC Portlet插件工程开发
官网上的教材说实话实在精简不清晰. https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/creating-an-mvc-por ...