Datawhale-LeetCode集训打卡-寻找两个有序数组的中位数
2019-01-28 17:07
549 查看
Datawhale-LeetCode集训打卡-寻找两个有序数组的中位数
Datawhale-LeetCode集训打卡-第二天-寻找两个有序数组的中位数
class Solution: def findMedianSortedArrays(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: float """ if len(nums1) > len(nums2): nums1, nums2 = nums2, nums1 m = len(nums1) n = len(nums2) low = 0 high = m while low <= high: # basic selection of the partitioner partitionM = (low + high) // 2 # partitioning of the second array partitionN = (m + n + 1) // 2 - partitionM # getting values. sometimes maxLeftM index can be 0 if partitionM > 0: maxLeftM = nums1[partitionM - 1] else: maxLeftM = float('-inf') if partitionM == m: minRightM = float('inf') else: minRightM = nums1[partitionM] if partitionN > 0: maxLeftN = nums2[partitionN - 1] else: maxLeftN = float('-inf') if partitionN == n: minRightN = float('inf') else: minRightN = nums2[partitionN] # let's check if partition was selected correctly if maxLeftM <= minRightN and minRightM >= maxLeftN: if (m + n) % 2 == 0: return (max(maxLeftM, maxLeftN) + min(minRightM, minRightN)) / 2.0 else: return max(maxLeftM, maxLeftN) # if not, move partitioning region elif maxLeftM > minRightN: high = partitionM -1 else: low = partitionM + 1
相关文章推荐
- LeetCode第4题--寻找两个有序数组的中位数
- Leetcode-寻找两个有序数组的中位数
- leetcode 寻找两个有序数组的中位数
- [leetcode 4] 寻找两个有序数组的中位数 python
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- LeetCode-4 :寻找两个有序数组的中位数 ★★★★★
- leetcode--寻找两个有序数组的中位数
- 【LeetCode】第四题:寻找两个有序数组的中位数
- Leetcode(C++)——4. 寻找两个有序数组的中位数
- 【leetcode】4. 寻找两个有序数组的中位数
- leetcode之数组类之数组的旋转与分治类-----OJ 189/33/81/153/154 数组旋转 旋转数组搜索 88 有序数组合并 4 两个有序数组寻找第K个元素/中位数 35 寻找插入位置
- [LeetCode]4. 寻找两个有序数组的中位数/C++
- leetcode 寻找两个有序数组的中位数 median of two sorted arrays
- LeetCode题解(python)-4. 寻找两个有序数组的中位数
- leetcode 4. 寻找两个有序数组的中位数(Java版)
- 寻找两个有序数组的中位数(LeetCode)
- 基础算法之寻找两个有序数组的中位数(LeetCode-4)
- LeetCode刷题-寻找两个有序数组的中位数
- LeetCode4.python实现:寻找两个有序数组中的中位数问题☆☆☆
- LeetCode--4. 寻找两个有序数组的中位数(Median of Two Sorted Arrays)