4. Median of Two Sorted Arrays
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)).
You may assume nums1 and nums2 cannot be both empty.
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
要求两个已经排好序的数列的中位数。中位数的定义:如果数列有偶数个数,那么中位数为中间两个数的平均值;如果数列有奇数个数,那么中位数为中间的那个数。
最直接的思路就是将两个数列合并在一起,然后排序,然后找到中位数就行了。可是这样最快也要O((m+n)log(m+n))的时间复杂度,而题目要求O(log(m+n))的时间复杂度。这道题其实考察的是二分查找.
二分查找法找两个数列的第k小个数:
即程序中getKth(A, B , k)函数的实现。用一个例子来说明这个问题:A = {1,3,5,7};B = {2,4,6,8,9,10};如果要求第7个小的数,A数列的元素个数为4,B数列的元素个数为6;k/2 = 7/2 = 3,而A中的第3个数A[2]=5;B中的第3个数B[2]=6;而A[2]<B[2];则A[0],A[1],A[2]中必然不可能有第7个小的数。因为A[2]<B[2],所以比A[2]小的数最多可能为A[0], A[1], B[0], B[1]这四个数,也就是说A[2]最多可能是第5个大的数,由于我们要求的是getKth(A, B, 7);现在就变成了求getKth(A’, B, 4);即A’ = {7};B不变,求这两个数列的第4个小的数,因为A[0],A[1],A[2]中没有解,所以我们直接删掉它们就可以了。这个可以使用递归来实现。
注意最后乘0.5的原因是这样才能得到float类型,如果是/2,会得到整形
class Solution(object):
def getKth(self, A, B, k):
lenA = len(A)
lenB = len(B)
if lenA > lenB:
return self.getKth(B, A, k)
if len(A) == 0:
return B[k-1]
if k == 1:
return min(B[0], A[0])
pa = min(lenA, k/2)
pb = k - pa
if A[pa-1] <= B[pb-1]:
return self.getKth(A[pa:], B, pb)
else:
return self.getKth(A, B[pb:], pa)
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
tmp = len(nums1) + len(nums2)
if tmp % 2 == 1:
return self.getKth(nums1, nums2, tmp/2+1)
else:
return (self.getKth(nums1, nums2, tmp/2) + self.getKth(nums1, nums2, tmp/2+1)) * 0.5