zoukankan      html  css  js  c++  java
  • LeetCode Hard: 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)).

    Example 1:

    nums1 = [1, 3]
    nums2 = [2]
    
    The median is 2.0

    Example 2:

    nums1 = [1, 2]
    nums2 = [3, 4]
    
    The median is (2 + 3)/2 = 2.5

    题意:给定两个有序的数组,找到两个数组的中位数。总的时间复杂度不能超过O(log(m+n))
    二、思路
    思路一、合并两个有序数组,然后找合并之后数组的中位数。下面的Solution0是做自己写的,看到了网上同样的思路,但是代码量少了好多,见Solution1;
    思路二、二分查找,递归实现   首先我们来看如何找到两个数列的第k小个数,即程序中getKth(A, B , k)函数的实现。用一个例子来说明这个问题:A = {1,3,5,7};B = {2,4,6,8,9,10};如果要求第7个小的数,A数列的元素个数为4,B数列的元素个数为6;k/2 = 7/2 = 3,而A中的第3个数A[2]=5;B中的第3个数B[2]=6;而A[2]<B[2];则A[0],A[1],A[2]中必然不可能有第7个小的数。因为A[2]<B[2],所以比A[2]小的数最多可能为A[0], A[1], B[0], B[1]这四个数,也就是说A[2]最多可能是第5个大的数,由于我们要求的是getKth(A, B, 7);现在就变成了求getKth(A', B, 4);即A' = {7};B不变,求这两个数列的第4个小的数,因为A[0],A[1],A[2]中没有解,所以我们直接删掉它们就可以了。这个可以使用递归来实现。
    三、代码
    #coding:utf-8
    class Solution0:
        def findMedianSortedArrays(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            """
            newnum = [None]*(len(nums1)+len(nums2))
            if nums1 == None or nums2 == None:
                return
            nums1_point = len(nums1)-1
            nums2_point = len(nums2)-1
            new_point = len(nums1)+len(nums2)-1
            while nums1_point >= 0 and nums2_point >= 0:
                if num1[nums1_point] > num2[nums2_point]:
                    newnum[new_point] = nums1[nums1_point]
                    new_point-=1;nums1_point-=1
                else:
                    newnum[new_point] = nums2[nums2_point]
                    new_point -=1;nums2_point-=1
            while nums1_point >= 0:
                newnum[new_point] = nums1[nums1_point]
                new_point-=1;nums1_point-=1
            while nums2_point >= 0:
                newnum[new_point] = nums2[nums2_point]
                new_point-=1;nums2_point-=1
            print(newnum)
            if len(newnum) % 2 != 0:
                median = newnum[len(newnum)//2]
            else:
                median = (newnum[len(newnum)//2-1] + newnum[(len(newnum)//2)])/2
            print(median)
            return median
    class Solution1:
    def findMedianSortedArrays(self, nums1, nums2):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :rtype: float
    """
    for i in nums2:
    nums1.append(i)
    nums1.sort()
    k=len(nums1)
    if k%2==0:
    return (float(nums1[int(k/2-1)])+nums1[int(k/2)])/2
    else:
    return nums1[int((k-1)/2)]
    class Solution2:
    def getKth(self,A,B,k):
    lenA = len(A);lenB = len(B)
    if lenA > lenB: return self.getKth(B,A,k)
    if lenA == 0: return B[k-1]
    if k == 1: return min(A[0],B[0])
    pa = min(k//2,lenA);pb = k - pa
    if A[pa-1] <= B[pb-1]:
    return self.getKth(A[pa:],B,pb)
    else:
    return self.getKth(A,B[pb:],pa)
    def findMedianSortedArrays(self,A,B):
    lenA = len(A);lenB = len(B)
    if (lenA + lenB) % 2 == 1:
    print(self.getKth(A,B,(lenA+lenB)//2 + 1))
    return self.getKth(A,B,(lenA+lenB)//2 + 1)
    else:
    print((self.getKth(A,B,(lenA+lenB)//2) + self.getKth(A,B,(lenA+lenB)//2+1))*0.5)
    return (self.getKth(A,B,(lenA+lenB)//2) + self.getKth(A,B,(lenA+lenB)//2+1))*0.5
    if __name__ == '__main__': num1 = [1,2,8] num2 = [4,5,6,7,10] ss =Solution0() ss.findMedianSortedArrays(num1,num2)

    参考博客:https://blog.csdn.net/suibianshen2012/article/details/51842597      https://www.cnblogs.com/zuoyuan/p/3759682.html     http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/

                         

    既然无论如何时间都会过去,为什么不选择做些有意义的事情呢
  • 相关阅读:
    【华为云技术分享】手把手教你如何在ARM上源码编译Redis
    【华为云技术分享】Linux内核编程环境 (2)
    华为云MySQL 8.0正式商用,全新增强版开源利器强势来袭
    【转载】Mysql删除所有表不删除数据库方法
    【转载】使用Sublime Text 3的HTML-CSS-JS Prettify插件格式化代码
    【转载】Node.js学习笔记之一
    jquery分页插件pagination.js的使用
    Windows平台下搭建Git服务器
    js实现返回页面顶部
    交换机的级联和堆叠
  • 原文地址:https://www.cnblogs.com/xiaodongsuibi/p/9017833.html
Copyright © 2011-2022 走看看