【leetcode 数组】Median of Two Sorted Arrays
来源:互联网 发布:初学编程怎么学 编辑:程序博客网 时间:2024/03/29 01:46
题目:
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)).
Divide and Conquer Array Binary Search求两个已排序数组的中值,时间复杂度要求O(log(m+n))
分析:
这是个经典的问题,更一般的问题是:求两个已排序数组中第k大的值
- 最笨的方法:采用插入排序(二分查找优化),假如m>n,则将n插入到m中,时间复杂度是O(n*logm)。排好序后输出第k个
- 线性复杂度的方法:其实我们只是要找出第k大的数,所以只需要排序前面k个数,不用排完所有数。可以使用类似与merge sort的方法,设置两个指针Pa、Pb和一个计数变量num,Pa和Pb分别指向数组A和B的首位,比较二者,较小的就输出,并且指针往前移一位,num++;再比较二者,小的输出......如此迭代进行,直到num==k。 这种方法时间复杂度是O(k);【对本题来说为O((m+n)/2)即O(m+n)】
- O(log (m+n))方法:O(k)方法是一个一个比的,可以用更快的方法,要找到A和B数组中第k大的数,可以比较A[k/2-1]以及B[k/2-1],假如A[k/2-1]比较小,则说明A[0]~A[k/2-1]都小于第k大的数,删除这k/2个数,得到新的A'。接下来,问题转化为求A'和B中第k/2大的数........如此循环直到k/2=1.。 由 k/2^n=1推算得这个方法的复杂度是O(logk)。【对本题来说是O(log(m+n))】
可以这样: int ia=min(k/2,m),ib=k-ia; 比较A[ia] 与 B[ib]
代码参考:
class Solution {public: double findMedianSortedArrays(int A[], int m, int B[], int n) { //分m+n为偶数和奇数两种情况 int total=m+n; if(total & 0x1) //奇数 return find_kth( A, m, B, n,total/2+1); else return (find_kth( A, m, B, n,total/2)+find_kth( A, m, B, n,total/2+1))/2.0; }private: static int find_kth(int A[],int m,int B[],int n,int k){ //规定传入的参数m<=n if(m>n) return find_kth(B,n,A,m,k); if(m==0) return B[k-1]; //一个为空 if(k==1) return min(A[0],B[0]); int ia=min(k/2,m),ib=k-ia; if(A[ia-1]<B[ib-1]) return find_kth(A+ia,m-ia,B,n,k-ia); else if(A[ia-1]>B[ib-1]) return find_kth(A,m,B+ib,n-ib,k-ib); else return A[ia-1]; }};
0 0
- 【leetcode 数组】Median of Two Sorted Arrays
- [leetcode] 【数组】4. 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 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
- 写给自己的一封信
- CocosStudio学习【1】:环境搭建与介绍
- (五)Decorator模式学习
- leetcode - Longest Substring Without Repeating Characters
- MFC专用名词缩写
- 【leetcode 数组】Median of Two Sorted Arrays
- for(;;)与while(1)的区别
- codeforces Ilya Muromets 2014 NEERC Southern Subregional Contest F dp
- PHP数组
- linux安装jdk
- ubuntu 13.04 安装 JDK
- CodeForces-58A-Chat room
- vmware 下bridge NAT host-only的区别
- leetcode 258 Add Digits(java)