LeetCode 力扣题解(4)寻找两个正序数组的中位数

LeetCode 力扣题解(4)寻找两个正序数组的中位数,实现语言:Python,解决方法:逐一遍历。

一、题目:寻找两个正序数组的中位数

给定两个大小分别为 mn 的正序(从小到大)数组 nums1nums2。请你找出并返回这两个正序数组的 中位数

示例 1:

输入:nums1 = [1,3], nums2 = [2]
输出:2.00000
解释:合并数组 = [1,2,3] ,中位数 2

示例 2:

输入:nums1 = [1,2], nums2 = [3,4]
输出:2.50000
解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5

示例 3:

输入:nums1 = [0,0], nums2 = [0,0]
输出:0.00000

示例 4:

输入:nums1 = [], nums2 = [1]
输出:1.00000

示例 5:

输入:nums1 = [2], nums2 = []
输出:2.00000

提示:

  • n u m s 1. l e n g t h = = m nums1.length == m nums1.length==m
  • n u m s 2. l e n g t h = = n nums2.length == n nums2.length==n
  • 0 < = m < = 1000 0 <= m <= 1000 0<=m<=1000
  • 0 < = n < = 1000 0 <= n <= 1000 0<=n<=1000
  • 1 < = m + n < = 2000 1 <= m + n <= 2000 1<=m+n<=2000
  • − 1 0 6 < = n u m s 1 [ i ] , n u m s 2 [ i ] < = 1 0 6 -10^6 <= nums1[i], nums2[i] <= 10^6 106<=nums1[i],nums2[i]<=106

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二、题解:逐一遍历

逐一遍历,对两个数组进行逐一遍历,并且按照从小到大的顺序排序成一个没有重复元素的新数组,在新数组中寻找中位数,时间复杂度是 O ( m + n ) O(m+n) O(m+n),空间复杂度是 O ( m + n ) O(m+n) O(m+n)

使用 index1index2 控制列表 nums1nums2 的读取,nums_index 控制列表 nums_list 的存储

  • 如果 nums1[index1] 不在列表 nums_list 中,并且比 nums2[index2] 小,便将 nums1[index1] 添加到 nums_list[nums_index] 中,index1 +=1,nums_index += 1
  • 同理,如果 nums2[index2] 不在列表 nums_list 中,并且比 nums1[index1] 小,便将 nums2[index2] 添加到 nums_list[nums_index] 中,index2 +=1,nums_index += 1

以此重复,直到遍历完两个列表,如果列表 nums_list 的长度是 奇数 则直接放回列表的中间值,如果是 偶数 则返回两个中间值相加除以 2 的结果。

三、示例代码

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        length1 = len(nums1)
        length2 = len(nums2)
        nums_list = []

        index1 = 0
        index2 = 0
        nums_index = 0
        while True:
            if index1==length1 and index2==length2:
                break

            if index1 < length1:
                if nums1[index1:index1+1] == nums_list[nums_index:nums_index+1]:
                    index1 += 1
                elif index2==length2 or nums1[index1:index1+1]<=nums2[index2:index2+1]:
                    nums_list.append(nums1[index1])
                    index1 += 1
                    nums_index += 1

            if index2 < length2:
                if nums2[index2:index2+1] == nums_list[nums_index:nums_index+1]:
                    index2 += 1
                elif index1==length1 or nums2[index2:index2+1] <= nums1[index1:index1+1]:
                    nums_list.append(nums2[index2])
                    index2 += 1
                    nums_index += 1

        nums_legth = len(nums_list)
        x = nums_legth // 2
        s = 0.0
        if nums_legth % 2 == 0:
            s = (nums_list[x-1] + nums_list[x]) / 2.0
        else:
            s = nums_list[x] / 1.0

        return s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值