LeetCode - Median of Two Sorted Arrays
来源:互联网 发布:fc2免费视频域名设置 编辑:程序博客网 时间:2024/05/01 14:52
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)).
//k starts from 0, means kth.double findKthValue(int A[], int m, int B[], int n, int k) { int count = 0; ++k; int index_A = -1; int index_B = -1; //Handle the special cases if ((m <= 0) && (n <= 0)) { return 0; } else if (m <= 0) { return B[k - 1]; } else if (n <= 0) { return A[k - 1]; } bool isA = true; while (count < k) { if (((index_A + 1) < m) && ((index_B + 1) < n)) { if (A[index_A + 1] <= B[index_B + 1]) { isA = true; ++index_A; } else { isA = false; ++index_B; } } else if (index_A + 1 < m) { isA = true; ++index_A; } else { isA = false; ++index_B; } ++count; } return isA ? A[index_A] : B[index_B];}double findMedianSortedArrays(int A[], int m, int B[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() function if ((NULL == A) || (NULL == B)) { return -1; } return ((m + n) % 2 == 0) ? ((findKthValue(A, m, B, n, (m + n) / 2 - 1) + findKthValue(A, m, B, n, (m + n) / 2)) / 2.0) : (findKthValue(A, m, B, n, (m + n) / 2));}
- 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
- iOS UITextField 详解
- 开博啦
- 通过url访问servlet出现404错误
- java中常用的字符串的截取方法
- 求职笔试题集合
- LeetCode - Median of Two Sorted Arrays
- 方向键控制光标在table中跳动
- 【cocos2d-x入门实战】微信飞机大战之九:碰撞检测
- Unity3d Web Player 的server端联网配置
- 程序员编程艺术:第二章、字符串是否包含及匹配/查找/转换/拷贝问题
- 手动安装gcc4.2
- gcc assert ndebug
- java验证手机号格式是否正确
- 将会改变未来IT世界的十种编程语言(转)