[Leetcode] Median of Two Sorted Arrays (Java)
2013-12-26 09:57
295 查看
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)).
一共有m+n个数,求中位数,分奇偶两种情况:
1)奇数时,中位数为第(m+n)/2+1位;
2)偶数时,中位数为第(m+n)/2位与第(m+n)/2+1位的平均数。
接下来的问题就是求第k位的数的大小:
二分法分两个部分,每个部分尽量达到k/2大小,若不够,则取最大max,与另一个数组的k-max位比较,得出结果。
O(log (m+n)).
一共有m+n个数,求中位数,分奇偶两种情况:
1)奇数时,中位数为第(m+n)/2+1位;
2)偶数时,中位数为第(m+n)/2位与第(m+n)/2+1位的平均数。
接下来的问题就是求第k位的数的大小:
二分法分两个部分,每个部分尽量达到k/2大小,若不够,则取最大max,与另一个数组的k-max位比较,得出结果。
public class MedianofTwoSortedArrays { private double find(int A[],int B[],int aLow,int aHigh,int bLow,int bHigh,int k){ if(aHigh-aLow < bHigh-bLow) return find(B, A, bLow, bHigh, aLow, aHigh, k); if(bHigh == bLow-1) return A[aLow+k-1]; if(k == 1) return A[aLow]>B[bLow]?B[bLow]:A[aLow]; int bb = Math.min(k/2, bHigh-bLow+1); int aa = k - bb; if(B[bb+bLow-1] < A[aLow+aa-1]) return find(A, B, aLow, aHigh, bLow+bb,bHigh, k-bb); else if(B[bb+bLow-1] > A[aLow+aa-1]) return find(A, B, aLow+aa, aHigh, bLow, bHigh, k-aa); else return A[aLow+aa-1]; } public double findMedianSortedArrays(int A[], int B[]) { int med; boolean flag = true; if((A.length+B.length)%2==1) { med = (A.length+B.length)/2 +1; }else { med = (A.length+B.length)/2 ; flag = false; } if(flag) { if(A.length == 0 && B.length!=0) return B[med-1]; else if (A.length!=0 && B.length == 0) return A[med-1]; return find(A, B, 0, A.length-1, 0, B.length-1, med); } else { if(A.length == 0 && B.length!=0) return ((double)B[med]+B[med-1])/2; else if (A.length!=0 && B.length == 0) return ((double)A[med]+A[med-1])/2; return (find(A, B, 0, A.length-1, 0, B.length-1, med)+find(A, B, 0, A.length-1, 0, B.length-1, med+1))/2; } } public static void main(String[] args) { int[] a = {1,3,5,7,9,11}; int[] b = {2,4,6,8,10,12,14,16}; double result = new MedianofTwoSortedArrays().findMedianSortedArrays(a, b); System.out.println(result); } }
相关文章推荐
- LeetCode Median of Two Sorted Arrays(JAVA)
- 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays
- Leetcode-Median of Two Sorted Arrays (java)
- [LeetCode]Median of Two Sorted Arrays(Java)
- [LeetCode][Java] Median of Two Sorted Arrays
- Leetcode 第四题< Median of Two Sorted Arrays>(Java)
- LeetCode – Median of Two Sorted Arrays Java
- Java [leetcode 4] Median of Two Sorted Arrays
- Leetcode: Median of Two Sorted Arrays. java.
- [LeetCode][4]Median of Two Sorted Arrays解析 -Java实现
- 【leetcode with java】4 Median of Two Sorted Arrays
- 【LeetCode-面试算法经典-Java实现】【004-Median of Two Sorted Arrays(两个排序数组的中位数)】
- LeetCode, Median of Two Sorted Arrays, Java Solution, O(m+n), O(log(m+n))
- leetcode 004 Median of Two Sorted Arrays(java)
- LeetCode 4 — Median of Two Sorted Arrays (C++ Java Python)
- Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现
- (leetcode)4.两个有序数列中间的第n个数 Median of Two Sorted Arrays--Java
- Leetcode 4 Median of Two Sorted Arrays Java
- LeetCode : Median of Two Sorted Arrays [java]
- leetcode:Median of Two Sorted Arrays 【Java】