LeetCode#4 Median of Two Sorted Arrays (2)

转载 2015年07月09日 12:23:29
def findKth(self,a,b,k,aStart,aEnd,bStart,bEnd):
        aLen = aEnd - aStart + 1
        bLen = bEnd - bStart + 1
        
        if aLen == 0:
            return b[bStart+k]
        
        if bLen == 0:
            return a[aStart+k]
            
        if k == 0:
            if a[aStart] > b[bStart]:
                return b[bStart]
            else:
                return a[aStart]
                
        aMid = k*aLen/(aLen + bLen)
        bMid = k - aMid - 1
        
        aMid+=aStart
        bMid+=bStart
        
        if a[aMid] > b[bMid]:
            k-=(bMid - bStart + 1)
            aEnd = aMid
            bStart = bMid + 1
        else:
            k-= (aMid - aStart + 1)
            bEnd = bMid
            aStart = aMid + 1
        
        return self.findKth(a,b,k,aStart,aEnd,bStart,bEnd)
    
    
    def findMedianSortedArrays(self, nums1, nums2):
        lenN1 = len(nums1)
        lenN2 = len(nums2)
        
        if (len(nums1) + len(nums2)) % 2 == 0:
            return (self.findKth(nums1,nums2,(len(nums1)+len(nums2))/2,0,len(nums1)-1,0,len(nums2)-1) + self.findKth(nums1,nums2,(len(nums1)+len(nums2))/2 - 1,0,len(nums1)-1,0,len(nums2)-1) ) / 2.0
        else:
            return self.findKth(nums1,nums2,(len(nums1)+len(nums2))/2,0,len(nums1)-1,0,len(nums2)-1)


相关文章推荐

LeetCode(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 t...
  • fly_yr
  • fly_yr
  • 2015年08月22日 20:35
  • 1049

Leetcode刷题记—— 4. Median of Two Sorted Arrays(两有序数组的中位数)

一、题目叙述: There are two sorted arrays nums1 and nums2 of size m...

LeetCode (4)Median of Two Sorted Arrays

(4)Median of Two Sorted Arrays题目:两个已经排好序的数组nums1和nums2,大小分别为m和n,求两个已排数组中位数。总运行时间复杂度应该是O(log(m+n))。用例...

[LeetCode][4]Median of Two Sorted Arrays解析 -Java实现

Q: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the...

LeetCode 题目 4. Median of Two Sorted Arrays

题目原址 :点击打开链接   题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively...

leetcode #4: Median of Two Sorted Arrays

题目链接: Median of Two Sorted Arrays 这道题坑还是有点多的,刚看到的时候第一反应是:这不就是归并排序....还是图样啊。所以先这样提交了: class Sol...
  • wyxpku
  • wyxpku
  • 2016年07月31日 00:33
  • 127

[编程练习][Median of Two Sorted Arrays]<LeetCode-4>

题目来自leetcode https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted ar...
  • drowzju
  • drowzju
  • 2014年12月04日 20:45
  • 188

Leetcode problem 4:Median of Two Sorted Arrays

DescriptionThere are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median...

LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)

原题网址:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an...
  • jmspan
  • jmspan
  • 2016年05月01日 15:39
  • 526

Leetcode NO.4 Median of Two Sorted Arrays

本题题目要求如下: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode#4 Median of Two Sorted Arrays (2)
举报原因:
原因补充:

(最多只允许输入30个字)