Median of Two Sorted Arrays
来源:互联网 发布:易建联nba数据统计 编辑:程序博客网 时间:2024/06/04 19:59
注:本题是参考了网友的答案,但实在想不起来究竟来源于哪里,抱歉
double findKth(int a[], int m, int b[], int n, int k){//always assume that m is equal or smaller than nif (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]);//divide k into two partsint 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);elsereturn a[pa - 1];}class Solution{public:double findMedianSortedArrays(int A[], int m, int B[], int n){int total = m + n;if (total & 0x1)return findKth(A, m, B, n, total / 2 + 1);elsereturn (findKth(A, m, B, n, total / 2)+ findKth(A, m, B, n, total / 2 + 1)) / 2;}};
0 0
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of two sorted arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- median-of-two-sorted-arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- 程序员如何参与创业
- 使用Intel编译器SSA
- AFNetwork 作用和用法详解
- Object-C 声明属性为什么用下划线,代码规范和编程风格
- 王思聪生日“成堆”手机电脑送来宾 嘉宾美女如云
- Median of Two Sorted Arrays
- [ssh2]list判断空,HQL from类,@Entity,Write operations are not allowed in read-only mode
- 数组作为函数的参数
- Combinations
- fsck修复受损的文件系统
- Permutation Sequence
- Eclipse用svn管理Android项目问题
- 忘记NSURLConnection,拥抱NSURLSession吧!
- 跟名站学前端之mcdonalds