题目:找出这两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。
示例 1:
nums1 = [1, 3]
nums2 = [2]
则中位数是 2.0
示例 2:
nums1 = [1, 2]
nums2 = [3, 4]
则中位数是 (2 + 3)/2 = 2.5
方法一:清奇思路,空间换时间(本人写的)
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
pos_nums, neg_nums, newList = [], [], []
nums = nums1 + nums2
total_length = len(nums)
for value in nums:
if value < 0:
length = len(neg_nums)
value1 = abs(value)
if value1 > length - 1:
neg_nums.extend([0]*(value1-length+1))
neg_nums[value1] += 1
else:
length = len(pos_nums)
if value > length - 1:
pos_nums.extend([0]*(value-length+1))
pos_nums[value] += 1
neg_length = len(neg_nums)
for index in range(neg_length-1, -1, -1):
if neg_nums[index] is not 0:
newList.extend([-index]*neg_nums[index])
for index, v in enumerate(pos_nums):
if v is not 0:
newList.extend([index]*v)
half = total_length // 2
return (newList[half])/1.0 if total_length & 1 else (newList[half-1] + newList[half])/2.0
方法二:网上看的
def median(A, B):
m, n = len(A), len(B)
if m > n:
A, B, m, n = B, A, n, m
if n == 0:
raise ValueError
imin, imax, half_len = 0, m, (m + n + 1) / 2
while imin <= imax:
i = (imin + imax) / 2
j = half_len - i
if i < m and B[j-1] > A[i]:
# i is too small, must increase it
imin = i + 1
elif i > 0 and A[i-1] > B[j]:
# i is too big, must decrease it
imax = i - 1
else:
# i is perfect
if i == 0: max_of_left = B[j-1]
elif j == 0: max_of_left = A[i-1]
else: max_of_left = max(A[i-1], B[j-1])
if (m + n) % 2 == 1:
return max_of_left
if i == m: min_of_right = B[j]
elif j == n: min_of_right = A[i]
else: min_of_right = min(A[i], B[j])
return (max_of_left + min_of_right) / 2.0