leetcode Median of Two Sorted Arrays
2015-03-04 17:34
302 查看
题目:
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小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。
所以只要解决了第k小数的问题,原问题也得以解决。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B
的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A
和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:
如果A或者B为空,则直接返回B[k-1]或者A[k-1];
如果k为1,我们只需要返回A[0]和B[0]中的较小值;
如果A[k/2-1]=B[k/2-1],返回其中一个。
代码:
public double findMedianSortedArrays(int A[], int B[]) {
int total = A.length + B.length;
if (total % 2 == 1)
return findKth(A, A.length, B, B.length, total / 2 + 1);
else
return (findKth(A, A.length, B, B.length, total / 2) + findKth(A, A.length, B, B.length, total / 2 + 1)) / 2;
}
public double findKth(int a[], int m, int b[], int n, int k) {
//确保m小于n
if (m > n)
return findKth(b, n, a, m, k);
if (m == 0)
return b[k - 1];
if (k == 1)
return a[0] < b[0] ? a[0] : b[0];
int pa = m < k / 2 ? m : k / 2;
int pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(getSubArray(a, pa), m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, getSubArray(b, pb), n - pb, k - pb);
else
return a[pa - 1];
}
public int[] getSubArray(int a[], int length) {
int sub[] = new int[a.length - length];
for (int i = 0; i < sub.length; i++)
sub[i] = a[i + length];
return sub;
}
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小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。
所以只要解决了第k小数的问题,原问题也得以解决。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B
的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A
和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:
如果A或者B为空,则直接返回B[k-1]或者A[k-1];
如果k为1,我们只需要返回A[0]和B[0]中的较小值;
如果A[k/2-1]=B[k/2-1],返回其中一个。
代码:
public double findMedianSortedArrays(int A[], int B[]) {
int total = A.length + B.length;
if (total % 2 == 1)
return findKth(A, A.length, B, B.length, total / 2 + 1);
else
return (findKth(A, A.length, B, B.length, total / 2) + findKth(A, A.length, B, B.length, total / 2 + 1)) / 2;
}
public double findKth(int a[], int m, int b[], int n, int k) {
//确保m小于n
if (m > n)
return findKth(b, n, a, m, k);
if (m == 0)
return b[k - 1];
if (k == 1)
return a[0] < b[0] ? a[0] : b[0];
int pa = m < k / 2 ? m : k / 2;
int pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(getSubArray(a, pa), m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, getSubArray(b, pb), n - pb, k - pb);
else
return a[pa - 1];
}
public int[] getSubArray(int a[], int length) {
int sub[] = new int[a.length - length];
for (int i = 0; i < sub.length; i++)
sub[i] = a[i + length];
return sub;
}
相关文章推荐
- leetcode Median of Two Sorted Arrays
- [leetcode]Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- LeetCode: 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 Online Judge 题目C# 练习 - Median of Two Sorted Arrays
- leetCode_Median of Two Sorted Arrays
- LeetCode: Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- Median of Two Sorted Arrays-----LeetCode
- [LeetCode题解004]Median of Two Sorted Arrays
- LeetCode —— Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays