[Leetcode] Median of Two Sorted Arrays (Java)
来源:互联网 发布:sql注入过安全狗 编辑:程序博客网 时间:2024/05/18 02:05
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位比较,得出结果。
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);elsereturn 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);}}
0 0
- [Leetcode] Median of Two Sorted Arrays (Java)
- [LeetCode][Java] Median of Two Sorted Arrays
- Median of Two Sorted Arrays --leetcode java
- LeetCode : Median of Two Sorted Arrays [java]
- leetcode:Median of Two Sorted Arrays 【Java】
- [LeetCode]Median of Two Sorted Arrays(Java)
- leetcode Median of Two Sorted Arrays(Java)
- Leetcode-Median of Two Sorted Arrays (java)
- LeetCode – Median of Two Sorted Arrays Java
- [leetcode NO.2] Median of Two Sorted Arrays (JAVA)
- leetcode第四题:Median of Two Sorted Arrays (java)
- 【leetcode with java】4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays Java
- leetcode 004 Median of Two Sorted Arrays(java)
- LeetCode(java)4. Median of Two Sorted Arrays
- [LeetCode][4]Median of Two Sorted Arrays解析 -Java实现
- leetcode: 4. Median of Two Sorted Arrays (java)
- Leetcode 第四题< Median of Two Sorted Arrays>(Java)
- AJAX省市区三级联动下拉列表实现 JAVA开发
- 你好,2014
- tab键和空格键的区别
- 【GitHub】在Mac上配置/使用Github
- php验证码类
- [Leetcode] Median of Two Sorted Arrays (Java)
- csdn博客导出
- DBCC大全集之(适用版本MS SQLServer 2008 R2)----DBCC DROPCLEANBUFFERS从缓冲池中删除所有清除缓冲区
- HIVE中UDTF编写和使用
- 6 InnoDB相关的数据结构
- Delphi数据库的拼音检索(查询)方案-汉字转换成拼音
- Base: ACID外的另外一种选择
- DBCC大全集之(适用版本MS SQLServer 2008 R2)----DBCC FREEPROCCACHE
- .NET开发工具