LeetCode -- Median of Two Sorted Arrays
来源:互联网 发布:php json 中括号 编辑:程序博客网 时间:2024/06/07 02:03
求解两个有序数组的的中值,下面的方法的确巧妙:
class Solution { int findKmin(int k, int A[], int m, int B[], int n){ if(m == 0) return B[k -1]; if(n == 0) return A[k -1]; if(k == 1) return min(A[0], B[0]); if(m > n) return findKmin(k, B, n, A, m); int p = min(m, k/2), q = k - p; if(A[p - 1] <= B[q - 1]) { return findKmin(k - p, A + p, m - p, B, n); } else { return findKmin(k - q, A, m, B + q, n - q); } }public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int k = m + n; if(k & 1) { return findKmin(k/2 + 1, A, m, B, n); } else { return (findKmin(k/2 , A, m, B, n) + findKmin(k/2 + 1, A, m, B, n))/2.0; } }};
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
- 9.7 Git 内部原理 - 维护及数据恢复
- Android菜单简析02(ContextMenu)
- 1.信号处理之:kill(),alarm(),pause()函数
- 图解SQL多表关联查询
- cf Two Sets (我用二分最大匹配做的)
- LeetCode -- Median of Two Sorted Arrays
- 一阶二阶电路滤波器的软件方式实现
- JAVA输入输出总结
- 一个字符串过滤程序
- HDOJ 5023 A Corrupt Mayor's Performance Art
- HDOJ 5024 Wang Xifeng's Little Plot
- cf 24 Game (观察+.. 想一想)
- 人生感想
- 2信号处理之:信号产生原因,进程处理信号行为,信号集处理函数,PCB的信号集,sigprocmask()和sigpending(),信号捕捉设定,sigaction,C标准库信号处理函数,可重入函数,