leetcode 日经贴,python code -Median of Two Sorted Arrays
2015-02-17 13:49
477 查看
Median of Two Sorted Arrays
class Solution: # @return an element from A or B def findKth(self, A, B, k): n, m = len(A), len(B) if n == 0: return B[k - 1] if m == 0: return A[k - 1] mid1, mid2 = n / 2, m / 2 if mid1 + mid2 + 2 > k: if A[mid1] >= B[mid2]: return self.findKth(A[:mid1], B, k) else: return self.findKth(A, B[:mid2], k) else: if A[mid1] >= B[mid2]: return self.findKth(A, B[mid2 + 1:], k - mid2 - 1) else: return self.findKth(A[mid1 + 1:], B, k - mid1 - 1) # @return a float def findMedianSortedArrays(self, A, B): n = len(A) + len(B) if n % 2 == 0: return (self.findKth(A, B, n / 2) + self.findKth(A, B, n / 2 + 1)) / 2.0 else: return self.findKth(A, B, n / 2 + 1)
相关文章推荐
- **Leetcode_median-of-two-sorted-arrays (c++ and python version)
- [leetcode]Median of Two Sorted Arrays @ Python
- LeetCode 4 — Median of Two Sorted Arrays (C++ Java Python)
- [Leetcode]4. Median of Two Sorted Arrays @python
- leetcode 日经贴,Cpp code -Median of Two Sorted Arrays
- 【LeetCode with Python】 Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays The Solution of Python
- 4.Median of Two Sorted Arrays Leetcode Python
- leetcode Median of Two Sorted Arrays python
- [LeetCode]题解(python):004-Median of Two Sorted Arrays
- [LeetCode]2 Median of Two Sorted Arrays (C++,Python实现)
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
- Median of Two Sorted Arrays -- leetcode -- Python
- leetcode python - 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