2020-02-26 Day4 Leetcode:4. Medi

2020-02-26  本文已影响0人  YueTan

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.


class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        
        result=[]
        i,j=0,0
        while i<len(nums1) and j<len(nums2):
            if nums1[i]<nums2[j]:
                result.append(nums1[i])
                i+=1
            else:
                result.append(nums2[j])
                j+=1
        while i<len(nums1):
            result.append(nums1[i])
            i+=1
        while j<len(nums2):
            result.append(nums2[j])
            j+=1
        
        if len(result)%2==1:
            return result[len(result)//2]
        else:
            return (result[len(result)//2]+result[len(result)//2-1])/2
        
                
        
上一篇 下一篇

猜你喜欢

热点阅读