LeetCode#4 Median of Two Sorted Arrays

来源:互联网 发布:landmark软件介绍 编辑:程序博客网 时间:2024/05/16 09:17

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)).


class Solution:    # @param {integer[]} nums1    # @param {integer[]} nums2    # @return {float}    def findMedianSortedArrays(self, nums1, nums2):        i = 0        j = 0         k = 0        newArr = []                while i < len(nums1) and j < len(nums2):            if nums1[i]<=nums2[j]:                newArr.append(nums1[i])                i+=1                k+=1            else:                newArr.append(nums2[j])                j+=1                k+=1                        while i < len(nums1):            newArr.append(nums1[i])            i+=1            k+=1        while j < len(nums2):            newArr.append(nums2[j])            j+=1            k+=1                    num = len(nums1) + len(nums2)                if  num % 2 == 1:            return newArr[num/2]        else:            return float(newArr[num/2-1]+newArr[num/2])/2                                    


0 0