【LeetCode】打卡–Python3算法4. 寻找两个有序数组的中位数
题目
给定两个大小为 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
结果
成功
显示详情
执行用时 : 76 ms, 在Median of Two Sorted Arrays的Python3提交中击败了100.00% 的用户
内存消耗 : 13.4 MB, 在Median of Two Sorted Arrays的Python3提交中击败了1.41% 的用户
解答
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
nums = nums1 + nums2
nums.sort()
if(len(nums)%2!=0):
return nums[int(len(nums)/2)]
else:
return (nums[int(len(nums)/2)] + nums[int(len(nums)/2 - 1)])/2
我们下次再见,如果还有下次的话!!!
欢迎关注微信公众号:516数据工作室

本文介绍了使用Python3解决LeetCode算法题4,寻找两个有序数组的中位数的问题,详细解释了题目要求和解题思路,时间复杂度为O(log(m+n))。
518

被折叠的 条评论
为什么被折叠?



