zoukankan      html  css  js  c++  java
  • LeetCode:4. 寻找两个有序数组的中位数

    1、题目描述

    给定两个大小为 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

    2、题解

    2.1、解法一

      原理:将nums1和nums2按照由小到大的顺序合并到nums数组中

      算法的时间复杂度: O(m)或O(n)

    class Solution:
        def findMedianSortedArrays(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            """
            
            l1 = len(nums1)
            l2 = len(nums2)
            print(nums1,nums2)
            nums = []
            if l1 and l2:
                if nums1[l1-1] <= nums2[0]:
                    nums.extend(nums1)
                    nums.extend(nums2)
                elif nums2[l2-1] <= nums1[0]:
                    nums.extend(nums2)
                    nums.extend(nums1)
                else:
                    index1 = 0
                    index2 = 0
    
                    while True:
                        if index1+1 > l1:
                            nums.extend(nums2[index2:])
                            break
                        if index2+1 > l2:
                            nums.extend(nums1[index1:])
                            break
    
                        if nums1[index1] <= nums2[index2]:
                            nums.append(nums1[index1])
                            index1 += 1
                        else:
                            nums.append(nums2[index2])
                            index2 += 1
                    
            elif l1>0 and l2 == 0:
                nums = nums1
            elif l1 == 0 and l2 >0:
                nums = nums2
            
            print(nums)
            l3 = len(nums)
            if l3% 2 == 1:
                mid = int(l3/2)
                ret = nums[mid]
            else:
                mid = int(l3/2 -1)
                ret = (nums[mid] + nums[mid+1])/2
            return ret
    

    2.2、解法二

      原理:将nums1和nums2合并后排序,然后求中位数

      算法的时间复杂度: O(m+(m+n)log(m+n))

    class Solution:
        def findMedianSortedArrays(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            """
            
            nums = nums1.extend(nums2)
            
            s = sorted(nums)
            
            if len(s) %2 == 1:
                ret = s[int(len(s)/2)]
            else:
                mid = int(len(s)/2 -1)
                ret = (s[mid] + s[mid+1])/2
            return ret
    

      

  • 相关阅读:
    任务18格式化
    任务17分区
    任务16 BIOS与CMOS
    任务15硬件组装过程说明
    任务14选配机箱
    任务13选配电源
    任务12选配显卡
    任务11选配机械硬盘
    任务10选配固态硬盘
    Android自定义控件:动画类(八)----ObjectAnimator基本使用
  • 原文地址:https://www.cnblogs.com/bad-robot/p/10064630.html
Copyright © 2011-2022 走看看