def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
n = len(nums1) + len(nums2)
if n % 2 == 1:
return self.findKth(nums1, nums2, int(n / 2) + 1) * 1.0
else:
smaller = self.findKth(nums1, nums2, int(n / 2))
bigger = self.findKth(nums1, nums2, int(n / 2) + 1)
return (smaller + bigger) / 2.0
def findKth(self, A, B, k):
print(A, B, k)
if len(A) == 0:
return B[k - 1]
if len(B) == 0:
return A[k - 1]
if k == 1:
return min(A[0], B[0])
a = A[int(k / 2) - 1] if len(A) >= k / 2 else None
b = B[int(k / 2) - 1] if len(B) >= k / 2 else None
if b is None or (a is not None and a < b):
return self.findKth(A[int(k / 2):], B, k - int(k / 2))
return self.findKth(A, B[int(k / 2):], k - int(k / 2))
Leetcode 之 Median of Two Sorted Arrays
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- https://leetcode.com/problems/median-of-two-sorted-arrays...
- Description There are two sorted arrays nums1 and nums2 o...
- 题目 There are two sorted arrays nums1 and nums2 of size m ...
- There are two sorted arrays nums1 and nums2 of size m and...
- 再让我说一段正经的话,你不远走,我不离去。 311. 你有一秒没说话 你成了谁 我在我的心上,画了好久你的样子 3...