[LeetCode]Median of Two Sorted Arrays
来源:互联网 发布:出租车打车软件 编辑:程序博客网 时间:2024/06/05 11:06
Description:
There are two sorted arrays nums1 and nums2 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)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
解析:这道题是求2个排序数组的中位数。一开始想到了合在一起然后找出中位数,这样比较简单,但是确不满足时间复杂度O(log (m+n))的要求。
其实这道题可以转化一下:寻找一个unioned sorted array中的第k大(从1开始数)的数。因而等价于寻找并判断两个sorted array中第k/2(从1开始数)大的数。
特殊化到求median,那么对于奇数来说,就是求第(m+n)/2+1(从1开始数)大的数。
而对于偶数来说,就是求第(m+n)/2大(从1开始数)和第(m+n)/2+1大(从1开始数)的数的算术平均值。
那么如何判断两个有序数组A,B中第k大的数呢?
我们需要判断A[k/2-1]和B[k/2-1]的大小。
如果A[k/2-1]==B[k/2-1],那么这个数就是两个数组中第k大的数。
如果A[k/2-1] < B[k/2-1], 那么说明A[0]到A[k/2-1]都不可能是第k大的数,所以需要舍弃这一半,继续从A[k/2]到A[A.length-1]继续找。当然,因为这里舍弃了A[0]到A[k/2-1]这k/2个数,那么第k大也就变成了,第k-k/2个大的数了。
如果 A[k/2-1]>B[k/2-1],
就做之前对称的操作就好。
这样整个问题就迎刃而解了。
这里要注意两个边界的判断:数组为empty,以及k==1时候的情况。
如果数组为empty:则直接返回第k个数。
如果k==1,则返回两个数组的最小值。
因为除法是向下取整,对每个数组的分半操作采取:
int partA = Math.min(k/2,m);int partB = k - partA;
为了能保证上面的分半操作正确,需要保证A数组的长度小于B数组的长度。
同时,在返回结果时候,注意精度问题,返回double型的就好。
Code:
public static double findMedianSortedArrays(int A[], int B[]) { int m = A.length; int n = B.length; int total = m+n; if (total%2 != 0) return (double) findKth(A, 0, m-1, B, 0, n-1, total/2+1);//k传得是第k个,index实则k-1 else { double x = findKth(A, 0, m-1, B, 0, n-1, total/2);//k传得是第k个,index实则k-1 double y = findKth(A, 0, m-1, B, 0, n-1, total/2+1);//k传得是第k个,index实则k-1 return (double)(x+y)/2; } } public static int findKth(int[] A, int astart, int aend, int[] B, int bstart, int bend, int k) { int m = aend - astart + 1; int n = bend - bstart + 1; if(m>n) return findKth(B,bstart,bend,A,astart,aend,k); if(m==0) return B[k-1]; if(k==1) return Math.min(A[astart],B[bstart]); int partA = Math.min(k/2,m); int partB = k - partA; if(A[astart+partA-1] < B[bstart+partB-1]) return findKth(A,astart+partA,aend,B,bstart,bend,k-partA); else if(A[astart+partA-1] > B[bstart+partB-1]) return findKth(A,astart,aend,B,bstart+partB,bend,k-partB); else return A[astart+partA-1]; }
参考:http://www.cnblogs.com/springfor/p/3861890.html
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 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] Median of Two Sorted Arrays
- [leetcode] 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
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- 第31课: Spark资源调度分配内幕天机彻底解密:Driver在Cluster模式下的启动、两种不同的资源调度方式源码彻底解析、资源调度内幕总结
- opencv 稀疏光流 稠密光流
- datatable导出CSV
- 二叉树的前中后序遍历的模板实现
- ObjC Runtime 中 Weak 属性的实现 (中)
- [LeetCode]Median of Two Sorted Arrays
- Android文件存储--采用SharedPreferences保存用户偏好设置
- 2017最新win系统下Zcash挖矿(ZEC挖矿)零币CPU挖矿教程【亲测有图有真相】
- 侦测USB插入动作,不用侦测盘符
- HDU3421 Max Sum II【序列处理】
- 填坑计划(持续更新中)
- [leetCode刷题笔记]199. Binary Tree Right Side View
- sql server 数据库恢复挂起
- java中List的用法和实例详解