[leetcode] Median of Two Sorted Arrays

摘要 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)).



https://oj.leetcode.com/problems/median-of-two-sorted-arrays/

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)).

思路1:两个数组进行merge操作,虽然不符合题目要求,但竟然拿可以过(有人说排序之后取中间的也能过。。)。复杂度O(m+n)。

思路2(参考讨论区某大神):使用二分查找,时间复杂度为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小值,所以我们可以将 其抛弃。 

 
double findKth(int a[], int m, int b[], int n, int k)
{
//always assume that m is equal or smaller than n
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]);
//divide k into two parts
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 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);
else
return (findKth(A, m, B, n, total / 2)
+ findKth(A, m, B, n, total / 2 + 1)) / 2;
}
};





java改写:java不支持数组后面元素取址操作,所以有点不方便,要么每次复制数组的一部分,要么增加参数,传入一个本次操作数组的范围,这里偷懒采用前一种方案。 

 
public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
int total = A.length + B.length;
if (total % 2 == 1)
return findKth(A, 0, B, 0, total / 2 + 1);
else
return (findKth(A, 0, B, 0, total / 2) + findKth(A, 0, B, 0, total / 2 + 1)) / 2;

}

private double findKth(int[] a, int i, int[] b, int j, int k) {
if (a.length > b.length)
return findKth(b, j, a, i, k);

if (a.length == 0)
return b[k - 1];

if (k == 1)
return Math.min(a[0], b[0]);

int pa = Math.min(k / 2, a.length), pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(Arrays.copyOfRange(a, pa, a.length), i + pa, b, j, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, i, Arrays.copyOfRange(b, pb, b.length), j + pb, k - pb);
else
return a[pa - 1];

}

public static void main(String[] args) {
int[] a = new int[] { 1, 3, 5, 7 };
int[] b = new int[] { 2, 4, 6, 8 };
System.out.println(new Solution().findMedianSortedArrays(a, b));

}
}
 

参考:

http://blog.csdn.net/yutianzuijin/article/details/11499917

http://blog.sina.com.cn/s/blog_62fc96e60101f8aa.html
转载自:http://my.oschina.net/jdflyfly/blog/283267



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值