LeetCode#4 Median of Two Sorted Arrays (2)
来源:互联网 发布:mysql查看字符集 编辑:程序博客网 时间:2024/06/05 04:56
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)
0 0
- Leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [leetcode 4] Median of Two Sorted Arrays
- LeetCode 4:《Median of Two Sorted Arrays》
- [Leetcode] 4 - Median of Two Sorted Arrays
- leetcode|4|Median of Two Sorted Arrays
- [Leetcode]4Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode #4 Median of Two Sorted Arrays
- LeetCode-4-Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- Leetcode[4] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode 4 -- Median of Two Sorted Arrays
- SAX PULL解析实例
- Caffe中增加新的layer以及Caffe中triplet loss layer的实现
- linux awk命令详解
- objective C中的浅复制和深复制
- jQuery插件开发
- LeetCode#4 Median of Two Sorted Arrays (2)
- 分布式缓存
- 从github上克隆的项目关联Git
- TextView中ellipsize属性
- 套接字地址
- setsockopt()用法(参数详细说明)
- Django:ExtendsNode: extends 'base/base.html'> must be the first tag in the template.
- X264移植android问题记录(No working C compiler found)
- 南阳oj 题目56 阶乘因式分解(一)