[leetcode NO.2] Median of Two Sorted Arrays (JAVA)
来源:互联网 发布:杨幂同款冬季拖鞋淘宝 编辑:程序博客网 时间:2024/06/05 18:31
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)).
据说这题在leetcode中是个5星题, 刚开始我用merge的方法,时间是O(m+n)(主要是没看清题目),居然也通过了, 我就没管了。 但是一个同学说不会, 我仔细看了下题目,居然要求O(lg(m+n)), 果断我也不会。网上查了资料后才知道答案。
下面提供些方法:
1.merge的方法: O(m+n)
2.用类似指针的方法,找到第k小的数: k刚开始指向两个数组的前面,比较a[i], b[j], 谁小,谁的index++, 同时k++, 当k到达给出的数后, 停止,同时知道k最后指向的数组就ok。这个方法要O(K)
3. 学名叫做二分搜索法,要和find kth smallest element in 2 sorted array 一起使用。这个问题就转化为找到 k = (m+n) / 2 的问题了
下面详细分析下find kth smallest element in 2 sorted array
比较 a[k/2 -1 ] 和 b[k/2-1], 会出现3种情 > < =.
if(a[k/2 -1 ] < b[k/2-1]) 说明 a[0]...到a[k/2-1]肯定属于两数组有序合并后的前k个,可以用反证法, 自己慢慢悟吧!! 这个时候我们可以将a[0 ... k/2 -1] 丢弃, 那么我们在剩下的 a[k/2 ... n]中和 b[0 ... m]中找 k - k/2 小的数, 就这样递归, 一直到退出。 注意退出的条件很多, 具体见代码
对于.> ,对b数组有同样的效果
if(a[k/2 -1 ] == b[k/2 -1 ]), 那么a[k/2 -1 ] 或者 b[k/2 - 1] 就是第k小的数。
下面是C++写的, 因为c++有指针,比较方便
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);elsereturn (findKth(A, m, B, n, total / 2)+ findKth(A, m, B, n, total / 2 + 1)) / 2;}double findKth(int a[], int m, int b[], int n, int k) {//总是使 a[]的长度小于b[]的if (m > n)return findKth(b, n, a, m, k);//递归退出情况有两种:// 1. a数组全部用完了,这时只有b数组了, 那么相当于在b中找到第K-1个元素即可,k是当前传递下来的// 2. k==1 的时候,那么找出a,b中最小的即可if (m <= 0)return b[k - 1];if (k == 1)return min(a[0], b[0]);//divide k into two parts,这时个细节问题, 当k/2大于m的时候, a要取全部的元素。int pa = min(k / 2, m), pb = k - pa;if (a[pa - 1] < b[pb - 1]) //这个时候我们可以将a[0 ... pa -1] 丢弃, 那么我们在剩下的 a[pa ... n]中和 b[0 ... m]中找 k -pa 小的数,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);elsereturn a[pa - 1]; }};
因为主要想以java来做题, 所以我吧java的代码贴出来, 完全是按照上面的思路改的,没有指针有时候真的很麻烦!!
public class Solution { public double findMedianSortedArrays(int A[], int B[]) { int k = A.length + B.length; return k % 2 == 0 ? (findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2 + 1) + findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2)) / 2 : findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2 + 1); } //返回两个数组中第k大的元素。 public double findK(int a[], int s1, int e1, int b[], int s2, int e2, int k) { int m = e1 - s1 + 1; int n = e2 - s2 + 1; if (m > n) return findK(b, s2, e2, a, s1, e1, k); //a的长度比b的小。 if (s1 > e1) return b[s2 + k - 1]; if (k == 1) return Math.min(a[s1], b[s2]); int midA = Math.min(k/2, m), midB = k - midA; //如果a的第midA大的元素比b的第midB大的元素小, //那么删掉a的前midA个元素,在剩余的数中找第k-midA大的。 if (a[s1 + midA - 1] < b[s2 + midB - 1]) return findK(a, s1 + midA, e1, b, s2, e2, k - midA); else if (a[s1 + midA - 1] > b[s2 + midB - 1]) return findK(a, s1, e1, b, s2 + midB, e2, k - midB); else return a[s1 + midA - 1]; }}
- [leetcode NO.2] Median of Two Sorted Arrays (JAVA)
- [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 2 MEDIAN OF TWO SORTED ARRAYS
- Leetcode【2】: Median of Two Sorted Arrays
- [leetcode 2] Median of Two Sorted Arrays
- Leetcode NO.4 Median of Two Sorted Arrays
- LeetCode No.4 Median of Two Sorted Arrays
- LeetCode – 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
- struts2 action向jsp传值
- 应用程序开发总结(9)--表单自动完成
- C++ primer 第五版 中文版 练习 10.2 个人code
- Mybatis获取插入记录的自增长ID
- libcouchabase和libevent的恩怨
- [leetcode NO.2] Median of Two Sorted Arrays (JAVA)
- Installation Guide for Oracle WebLogic Server 11g Release 一 (10.3.3)
- 般若波罗蜜多心
- 为什么电脑卡,怎么解决?
- verilog阻塞与非阻塞的综合
- poj 1751 Highways
- 互联网创业梦已醒
- 追随内心
- 滚动视图的自动循环滚动