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...