1. 程式人生 > >python 求兩個有序數組合並後的中位數

python 求兩個有序數組合並後的中位數

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        if nums1==[]:
            res=nums2
        elif nums2==[]:
            res=nums1
        else:
            res=nums1+nums2
        res.sort()
        l=len(res)
        if l%2 == 1:
            return res[l/2]
        else:
            return (res[l/2-1]+res[l/2])/2.0