LeetCode2:Median of Two Sorted Arrays

题目:

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为偶数的话,应为中间两数之和除2,但时间复杂度不符合题目要求,还一种方法就是利用归并思想,因为两数组为排序数组,对两数组进行归并,当已归并个数为(m+n)/2时,即求得中间数,不用继续归并了,不过这种方式时间复杂度也不符合要求。那有没其他更快的方法呢。

这里要说的就是利用二分查找的方式,当要在排序数组中查找某个元素时,我们就应该朝二分法查找思考。二分法正是利用已排序数组这一特性快速查找。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。

实现代码:


#include <iostream>

using namespace std;

/**
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)).
*/
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int k = (m + n) / 2;
if((m + n) & 0x1)//奇数
return findKthNumber(A, m, B, n, k + 1);
return (findKthNumber(A, m, B, n, k) + findKthNumber(A, m, B, n, k+1)) / 2;

}
double findKthNumber(int A[], int m, int B[], int n, int k)
{
if(m > n)
return findKthNumber(B, n, A, m, k);//这里始终假设m<n
if(m == 0)
return B[k-1];
if(n == 0)
return A[k-1];
if(k == 1)
return A[0] < B[0] ? A[0] : B[0];
int ak = min(k / 2, m);
int bk = k - ak;
if(A[ak-1] < B[bk-1])
return findKthNumber(A+ak, m-ak, B, n, k-ak);
else
return findKthNumber(A, m, B+bk, n-bk, k-bk);

}
};
int main(void)
{
int A[] = {1,5,7,9,14,15};
int B[] = {3,6,10,13,18,20};
Solution s;
double res = s.findMedianSortedArrays(A, 6, B, 6);
cout<<res<<endl;
return 0;
}

LeetCode2:Median of Two Sorted Arrays,码迷,mamicode.com

时间: 2024-08-05 19:21:13

LeetCode2:Median of Two Sorted Arrays的相关文章

No.004:Median of Two Sorted Arrays

题目: 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)). Example1:nums1 = [1, 3]nums2 = [2]The median is 2.0Example2:nums1 = [1, 2]n

Q4:Median of Two Sorted Arrays

4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays 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

LeetCode 4: Median of Two Sorted Arrays

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)) 代码如下: class Solution { int getMedian(int A[], int m, int B[], int n,int k) { if

第四题:Median of Two Sorted Arrays

题目链接:题目链接 题意:两个排好序的数组,找到中位数,如果是奇数好办,如果是偶数找到最中间的两个求平均值. 这一题的本质其实就是第mid小的数. 这一题看到一种好的方法,我们令k=mid,对于两个数组,分别取前k/2数,如果A[k/2-1]比B[k/2-1]大,那么说明B前 k/2数肯定在k小数中,A的则不一定,则下面需要从A,B+k/2再次去寻找...反之对于B大的情况也是一样的,如果相 等,那就是这两个数(相等)了,随便返回一个都OK.(Ps:解释一些为什么是k/2-1,因为k是从1开始取

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: 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

LeetCode题解-----Median of Two Sorted Arrays

题目描述: 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)). 解题思路: 本题要求求解的是两个有序序列的中位数.本质上就是求两个有序序列“第k小的数“的变形.假设两个有序序列一个长为m,另一个长为n,则我们

leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数

题目: 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)). 题意: 两个排序后的数组nums1 和nums2,长度分别是m,n,找出其中位数,并且时间复杂度:O(log(m+n)) 最愚蠢的方法: 两个数组合

Leetcode 解题 Median of Two sorted arrays

题目: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)) 题解: 1.自己想得思路:构建一个list,然后比较各数的大小,将其插入到合适的位置 class Solution: # @param {integer[

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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