【leetcode刷题笔记】Median of Two Sorted Arrays
2014-07-26 17:12
423 查看
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)).
题解:注意这里对median的理解,如果A,B合并后的序列有奇数个元素,那么中间元素就是下标为(a.length+b.length)/2的元素;而如果合并后的序列有偶数个元素,那么median是下标为(a.length+b.length)/2和(a.length+b.length)/2-1两个元素的平均数。
我们实现一个二分在有序两个数组中找第k小的数的函数,然后在主函数中,根据合并后数组元素个数的奇偶性调用这个函数。
首先来看这个在两个有序数组中找第k小的数的函数 private double findKth(int a[],int b[],int k,int a_start,int b_start){ 。它的原理如下图所示:
即:
再来看求解函数 public double findMedianSortedArrays(int A[], int B[]) ,当A和B合并后元素个数为奇数的时候,我们直接调用 findKth(A, B, (B.length+A.length)/2+1, 0, 0) 找到第(A.length+B.length)/2的数就是中位数了。而当A和B的合并后元素个数为偶数的时候,我们要调用两次findKth分别找到第(A.length+B.length)/2和第(A.length+B.length)/2-1的数,然后求它们的平均数,即 (findKth(A, B, (A.length+B.length)/2, 0, 0) + findKth(A, B, (A.length+B.length)/2+1, 0, 0)) / 2.0 。
代码如下:
题解:注意这里对median的理解,如果A,B合并后的序列有奇数个元素,那么中间元素就是下标为(a.length+b.length)/2的元素;而如果合并后的序列有偶数个元素,那么median是下标为(a.length+b.length)/2和(a.length+b.length)/2-1两个元素的平均数。
我们实现一个二分在有序两个数组中找第k小的数的函数,然后在主函数中,根据合并后数组元素个数的奇偶性调用这个函数。
首先来看这个在两个有序数组中找第k小的数的函数 private double findKth(int a[],int b[],int k,int a_start,int b_start){ 。它的原理如下图所示:
即:
if(a_mid < b_mid) return findKth(a, b, k-k/2, a_start+k/2, b_start); else return findKth(a, b, k-k/2, a_start, b_start+k/2);
再来看求解函数 public double findMedianSortedArrays(int A[], int B[]) ,当A和B合并后元素个数为奇数的时候,我们直接调用 findKth(A, B, (B.length+A.length)/2+1, 0, 0) 找到第(A.length+B.length)/2的数就是中位数了。而当A和B的合并后元素个数为偶数的时候,我们要调用两次findKth分别找到第(A.length+B.length)/2和第(A.length+B.length)/2-1的数,然后求它们的平均数,即 (findKth(A, B, (A.length+B.length)/2, 0, 0) + findKth(A, B, (A.length+B.length)/2+1, 0, 0)) / 2.0 。
代码如下:
public class Solution { private double findKth(int a[],int b[],int k,int a_start,int b_start){ //if a is empty if(a_start >= a.length) return b[b_start+k-1]; if(b_start >= b.length) return a[a_start+k-1]; if(k == 1) return Math.min(a[a_start], b[b_start]); int a_mid = a_start + k/2 -1 < a.length?a[a_start+k/2-1]:Integer.MAX_VALUE; int b_mid = b_start + k/2 -1 < b.length?b[b_start+k/2-1]:Integer.MAX_VALUE; if(a_mid < b_mid){ return findKth(a, b, k-k/2, a_start+k/2, b_start); } else return findKth(a, b, k-k/2, a_start, b_start+k/2); } public double findMedianSortedArrays(int A[], int B[]) { int len = A.length + B.length; if(len % 2 == 0) return (findKth(A, B, len/2, 0, 0) + findKth(A, B, len/2+1, 0, 0)) / 2.0; else return findKth(A, B, len/2+1, 0, 0); } }
相关文章推荐
- (python)leetcode刷题笔记04 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: 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 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 26: 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