leetcode Median of Two Sorted Arrays
来源:互联网 发布:项目管理系统 源码 编辑:程序博客网 时间:2024/06/06 23:16
There are two sorted arrays A and B 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 {public:double findKth(int a[], int m, int b[], int n, int k){ if(m > n) return findKth(b, n, a, m, k); if(m == 0) return b[k-1]; if(k == 1) return min(a[0], b[0]); int pa = min(k/2, m), pb = k - pa; if(a[pa - 1] < b[pb - 1]) return findKth(a+pa, m-pa, b, n, k-pa); else if(a[pa - 1] > b[pb - 1]) return findKth(a, m, b+pb, n-pb, k-pb); else return b[pb-1];} double findMedianSortedArrays(int A[], int m, int B[], int n) { int sum = m + n; if(sum & 0x01) return findKth(A, m, B, n, sum / 2 + 1); else return ( findKth(A, m, B, n, sum / 2 ) + findKth(A, m, B, n, sum / 2 + 1) ) / 2; }};
0 0
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- hdu 3917 最大权闭合图
- Java Generic
- 华为面试题目
- leetcode Two Sum
- SQL基础语法-W3School
- leetcode Median of Two Sorted Arrays
- hdu 3068 最长回文 manacher算法
- 验证是否有重复数据
- ACM-大数N!的位数公式
- leetcode 3Sum Closest
- 计算机为什么要用补码?
- H2 Database doc 阅读记录
- 设计模式之简单工厂模式
- 电子钱包、电子现金与qPBOC