[Array] 004 Median of Two Sorted Arrays

  • 分类:Array

  • 考察知识点:Array(数组遍历) 二分法

  • 最优解时间复杂度:O(log(min(n,m)))

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

You may assume nums1 and nums2 cannot be both empty.

Example1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

代码:

最烂解法(我自己一开始想的):

主要Python直接有个sort方法就简化了整个问题变得十分的弱智。
就是把两个list加起来重新排序一下,然后把中位数取出来。

  • 时间复杂度:O(m+n)
class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums=nums1+nums2
        nums.sort()
        if len(nums)%2==0:
            return (nums[int(len(nums)/2-1)]+nums[int(len(nums)/2)])/2
        else:
            return nums[(int(len(nums)/2))]

最优解法:

reference:https://www.bilibili.com/video/av31234937/?p=4

  • 时间复杂度: O(log(min(m+n)))
class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        #获得我们所需要的中位数
        median_index=(len(nums1+nums2)//2)+(len(nums1+nums2)%2)

        #先区分长的nums和短的nums,方便之后的计算
        if len(nums1)>=len(nums2):
            nums_long,nums_short=nums1,nums2
        else:
            nums_long,nums_short=nums2,nums1

        if len(nums_short)==0:
        #在其中有一个array为0的时候
            nums_new=nums_long
        elif nums_short[-1]<=nums_long[0]:
        #当短数列全部小于长数列的时候
            nums_new=nums_short+nums_long
        elif nums_long[-1]<=nums_short[0]:
        #当长数列全部小于短数列的时候
            nums_new=nums_long+nums_short
        else:
        #其他情况,我们重头戏!我们要开始解了!
            short_index=len(nums_short)//2
            #短数列指针的位置
            short_range=len(nums_short)
            #短数列需要搞完的大小
            start=0
            #短数列的开头
            end=len(nums_short)
            #短数列的结尾
            while(short_range!=0):
                long_index=median_index-short_index
                #长数列指针的位置
                #给有些不存在的位置上的数赋值 比如 __[||2],给下划线的位置赋2-1
                if short_index-1<0:
                    nums_short_small=nums_long[long_index-1]-1
                else:
                    nums_short_small=nums_short[short_index-1]
                if long_index-1<0:
                    nums_long_small=nums_short[short_index-1]-1
                else:
                    nums_long_small=nums_long[long_index-1]
                if short_index>=len(nums_short):
                    nums_short_big=nums_long[long_index]+1
                else:
                    nums_short_big=nums_short[short_index]
                if long_index>=len(nums_long):
                    nums_long_big=nums_short[short_index]+1
                else:
                    nums_long_big=nums_long[long_index]
                #重头戏开始了,开始我们的二分法
                #当OJBK完结了!
                if nums_short_small<=nums_long_big and nums_long_small<=nums_short_big:
                    max_min=max(nums_short_small,nums_long_small)
                    min_max=min(nums_short_big,nums_long_big)
                    if len(nums1+nums2)%2==0:
                        return (max_min+min_max)/2
                    else:
                        return max_min
               #需要往右移动
                elif nums_long_small>nums_short_big:
                    start=short_index
                    short_range=end-start
                    short_index+=(short_range//2+short_range%2)
                else:
                #需要往左移动
                    end=short_index
                    short_range=end-start
                    short_index-=(short_range//2+short_range%2)


        if len(nums_new)%2==0:
            return (nums_new[median_index]+nums_new[median_index-1])/2
        else:
            return nums_new[median_index-1]

讨论:

1.这个题目我一开始是想用Python自带的解法解决,然后发现虽然能够解决,但是比较耗时,然后我就在互联网上搜了这道题的解法。
2.这个题的解法一共有三种,有兴趣的可以参考这篇博主写的文章:http://windliang.cc/2018/07/18/leetCode-4-Median-of-Two-Sorted-Arrays/
3.本来这个题我想用三种方法都搞一遍,然后我发现我写上面这篇博主的方法二的时候,就是那个找第k小的数的那个题,我死活代码有bug,就是有test-case通不过,而且竟然花了我2天的时间,实在太不值得了所以放弃了
4.看到时间复杂度含有log的一定要尝试用二分法解题!
5.array很多时候很考察对边界是否敏感,一定要注意临界点
6.二分法里面,二分的时候是否要+1也是非常敏感的,现在看来是都需要+1的

[Array] 004 Median of Two Sorted Arrays_第1张图片
我自己一开始的方法
[Array] 004 Median of Two Sorted Arrays_第2张图片
最优的方法

你可能感兴趣的:([Array] 004 Median of Two Sorted Arrays)