Leetcode4 寻找两个正序数组中位数

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        def Getkthsmall(nums1,start1,end1,nums2,start2,end2,k):
            len1=end1-start1+1 
            len2=end2-start2+1 
            if(len1>len2):
                #这样len1就会小于len2
                return Getkthsmall(nums2,start2,end2,nums1,start1,end1,k)
            if(len1==0):
                return nums2[start2+k-1]

            if(k==1): 
                return min(nums1[start1],nums2[start2])

            i = start1+min(len1,k//2)-1  #0 #1
            j = start2+min(len2,k//2)-1  #0

            if(nums1[i]>nums2[j]):
                return Getkthsmall(nums1,start1,end1,nums2,j+1,end2,k-(j-start2+1))
            else:
                return Getkthsmall(nums1,i+1,end1,nums2,start2,end2,k-(i-start1+1))
            
        
        
        n = len(nums1)  
        m = len(nums2)  
        left = (n+m+1)//2  
        right = (n+m+2)//2   

        
        return (Getkthsmall(nums1,0,n-1,nums2,0,m-1,left)+Getkthsmall(nums1,0,n-1,nums2,0,m-1,right))/2

过几天复盘

你可能感兴趣的:(算法,算法)