LeetCode:Median of Two Sorted Arrays
来源:互联网 发布:千兆路由器 知乎 编辑:程序博客网 时间:2024/06/06 04:17
LeetCode:Median of Two Sorted Arrays
The Problem is described as following:
There are two sorted arrays A and B 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))
.
My solution is as following:
class Solution: # @return a float def findMedianSortedArrays(self, A, B): if len(A)==0 and len(B)!=0: return B[len(B)/2] if len(B)%2!=0 else (B[len(B)/2-1]+B[len(B)/2])/2.0 if len(B)==0 and len(A)!=0: return A[len(A)/2] if len(A)%2!=0 else (A[len(A)/2-1]+A[len(A)/2])/2.0 if len(A)+len(B)==0: return None k = (len(A)+len(B)+1)/2 numofFirstPart = 0 # to store the num of first(smaller) part needtwoMedian = False if (len(A)+len(B)) %2 == 0 : needtwoMedian = True # means that we need to get the meddle 2 nums to cal the median while (k-numofFirstPart)/2-1 >= 0 : indexA = (k-numofFirstPart)/2-1 if len(A)>=(k-numofFirstPart)/2 else len(A)-1 indexB = (k-numofFirstPart)/2-1 if len(B)>=(k-numofFirstPart)/2 else len(B)-1 if A[indexA] < B[indexB]: numofFirstPart += indexA + 1 if indexA != len(A)-1: A = A[indexA+1:] else : A = [] return (B[k-numofFirstPart]+B[k-numofFirstPart-1])/2.0 if needtwoMedian else B[k-numofFirstPart-1] else : numofFirstPart += indexB + 1 if indexB != len(B)-1: B = B[indexB+1:] else: B = [] return (A[k-numofFirstPart]+A[k-numofFirstPart-1])/2.0 if needtwoMedian else A[k-numofFirstPart-1] if not needtwoMedian: return A[0] if A[0]<B[0] else B[0] else : temp = (A[0]+B[0]) /2.0 if len(A)>1 : if (A[0]+A[1])/2.0<temp: temp = (A[0]+A[1])/2.0 if len(B)>1 : if (B[0]+B[1])/2.0<temp: temp = (B[0]+B[1])/2.0 return temp
这道题目有一定的难度,这里记录一点解题的思路。
由于题目增加了时间复杂度为O(log(m+n))
的要求,使得简单暴力的方法不可行。这里作为的简单暴力的方法很简单,只需要设置两个指针,从A和B的头元素开始向后依次比较,向后移动相对小的值的指针,直到计数到(m+n)/2
为止。
基于时间复杂度的考虑,首先很自然地想到二分法类似的思路。然而起初打算分别针对两个Array每次截半比较,较小值得左端元素分到较小的一半,较大值右端元素分到较大的一半,(这里的一半就是以Median作为分界的),然而到了后面编程实现比较不方便,遂放弃。
转换思路,令k=(m+n)/2
,这样也就类似于寻找第k小的元素,依次确定k/2,k/4,k/8···1个元素,并用numofFirstPart记录确定为较小的一部分集合的元素的个数,当numofFirstPart达到k问题就解决了,从而实现题目的时间复杂度的要求。
具体地,首先针对两个数组分别选取A[k/2-1]
和B[k/2-1]
的元素(这里假设数组长度够长,若不够长,这直接取最后一个元素),也就是第k/2个元素,若A[k/2-1]<B[k/2-1]
,我们就抛弃B数组的前k/2个元素,并增大numofFirstPart的值;反之对A进行同样的处理。之后利用处理后的A和B进行同样的处理,此时选取的元素应该是第(k-numofFirstPart)/2
,即尽量做到每次都可以确定待确定元素的一半。接下来如此往复进行即可。
这里要注意几个细节:
- 如果某数组元素为0的情况
- 当选取元素时候数组现有元素的数目小于k/2的情况
- 两个数组元素数目和胃偶数的情况
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- cocos2d-x3.2 在Windows下打包Android平台APK
- 只允许ajax请求
- android控件之ListView 全教程
- 1.14
- 深入Spark内核:任务调度(3)-TaskScheduler
- LeetCode:Median of Two Sorted Arrays
- MySQL命令收集
- 页面缓存设置
- [转]设置修改CentOS系统时区/时间
- 信号量与PV操作
- MySQL5.6主从复制
- 使用eclipse查看hadoop源码
- MSVC下快速Unicode I/O
- 第六届南桥杯JavaA组 第五题 打印菱形